Android: Intent Service

497 views Asked by At

I am trying to obtain location by using Fused location provider api by implementing it in the intentservice class and at the same time i am starting a newtimertask in the same service.

so when intentservice gets called, the timertask starts and the googleapiclent gets connected to get the location.

i want that if the location is not availaible ,my timertask disconnects the googleapiclient in next 60 seconds.

But this doesnot work...if it doesnot get the location...the intent service keeps on running or the fusedlocatinprovider keeps for looking location.so i have to stop these?

package com.example.rj.intentlocationapi;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;


public class BackgrndService extends IntentService implements        LocationListener,
     GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
LocationManager mlocationmanger;
int bool = 0;
CountDownTimer cnt;


public BackgrndService() {
    super("BackgrndService");
}


protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

private boolean isGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (ConnectionResult.SUCCESS == status) {
        return true;
    } else {
        return false;
    }
}

@Override
protected void onHandleIntent(Intent intent) {
    if (!isGooglePlayServicesAvailable()) {
        stopLocationService();
    }
    mlocationmanger = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);


    createLocationRequest();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    cnt = new CountDownTimer(60000, 1000) {

        public void onTick(long millisUntilFinished) {

        }

        public void onFinish() {
            Log.w("haha", "going to finish it");
            stopLocationService();


        }
    }.start();
    mGoogleApiClient.registerConnectionCallbacks(this);
    mGoogleApiClient.connect();
    Log.w("haha", "blocked to finish it");


}

public void onConnected(Bundle bundle) {
    Log.w("hello", "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
    startLocationUpdates();
}

protected void startLocationUpdates() {
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {
    Log.w("hhaahaa", "Connection suspended stop here ");


}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.w("hhaahaa", "Connection failed stop here ");
}

@Override
public void onLocationChanged(Location location) {
    Log.w("hello", "Firing onLocationChanged..............................................");
    mCurrentLocation = location;
    if (mCurrentLocation != null) {
        Log.w("hi", mCurrentLocation.getLatitude() + "  " + mCurrentLocation.getLongitude());

    }
    stopLocationService();
}


public void stopLocationService() {

    Log.w("haha", "killing begins");

    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    mGoogleApiClient.disconnect();
    cnt.cancel();

    Intent alarm = new Intent(this, BackgrndService.class);


    PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarm, 0);

    if (bool == 0) {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()
                + 1000 * 100, pendingIntent);
        bool = 1;
    }

} }

2

There are 2 answers

3
Borra Suneel On BEST ANSWER

Either you will shift or change Intent-Service to Service and use stopSelf() method to stop the service.This method stop the service.

2
Borra Suneel On

IntentService automatically stops itself when onHandleIntent() ends, if no more commands had been sent to it while onHandleIntent() was running. Hence, you do not manually stop an IntentService yourself.