Naveen
Naveen

Reputation: 551

I cannot able to stop the service and BroadcastReceiver.I have tried the below code

Actually I want to update my location for every minute to the server, I have looked into so many links but none of them working fine for me.Finally I got this link

I have tried this link: https://deepshikhapuri.wordpress.com/2016/11/25/service-in-android/
for getting the location in a service.I am using the sourcecode from the link but I cannot able to stop the service and BroadcastReceiver

I am starting the Service and BroadcastReceiver like following code

    //Starting Service
    Intent intent = new Intent(getApplicationContext(), GoogleService.class);
                    startService(intent);

    //Starting BroadcastReceiver
    registerReceiver(broadcastReceiver, new IntentFilter(GoogleService.str_receiver));

I have tried the below code to stop service and BroadcastReceiver.

//Unregistering BroadcastReceiver
 LocalBroadcastManager.getInstance(ServiceLocActivity.this).unregisterReceiver(broadcastReceiver);

//stopping service
 Intent it = new Intent(getApplicationContext(), GoogleService.class);
 stopService(it);

The above code is not working. I cannot able to stop Service and Broadcast Receiver Can you please help me on this.Thanks in Advance.

Upvotes: 3

Views: 172

Answers (1)

Omar Hayat
Omar Hayat

Reputation: 378

As intent service automatically stops after completing its cycle. Put your latest lat and lon in global variable.

public class LocationService extends IntentService{
public Config sharedData;
public boolean logincase = true;
ExceptionSave ex;
Intent broadcastIntent;
public String journeyState="jr_init";
public static boolean performTasks = true;
private static final String TAG = "com.live.locationservices.LocationServic";

public LocationService() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
    if (this.performTasks) {
//If this variable is true just call your endpoint and send global variable lat and lon to server.

}
}
public class ResponseReceiver extends WakefulBroadcastReceiver {
    public static final String ACTION_RESP = "com.live.ResponseReceiver.intent.action.MESSAGE_PROCESSED";

    @Override
    public void onReceive(Context context, Intent intent) {
if(LocationServiceDriver.performTasks){
//Start your service again 1 mint post dealy
}
}

Unregistered your broadcast receiver on activity destroy.When you done your task just set LocationServiceDriver.performTasks=false

Upvotes: 2

Related Questions