3

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.

UltimateDevil
  • 2,807
  • 2
  • 18
  • 31
Naveen
  • 481
  • 2
  • 6
  • 23
  • What error you got in logcat.. – Sardar Khan Sep 28 '17 at 05:07
  • 1
    @SardarKhan, please check the logcat 09-28 10:49:33.785 883-970/? D/PerfServiceManager: [PerfService] MESSAGE_TIMEOUT:103 ? I/libPerfService: 3: set: 3 ? I/libPerfService: 3: set freq: 819000 ? I/libPerfService: 3: set vcore level: 0 ? I/PerfService: PerfServiceNative_getPackName ? I/libPerfService: perfGetLastBoostPid 5392 ? D/PerfServiceManager: [PerfService] getLastBoostPid 5392 ? I/libPerfService: perfGetLastBoostPid 5392 – Naveen Sep 28 '17 at 05:21
  • can you show the code where error occurs.? – Sardar Khan Sep 28 '17 at 07:38
  • Or you can show the full logcat error report its not clear from this that you getting which error. – Sardar Khan Sep 28 '17 at 07:39
  • Whether the service is bound to activity or not show the activity code here. – Sardar Khan Sep 28 '17 at 07:39

1 Answers1

2

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

Omar Hayat
  • 378
  • 2
  • 12
  • Hi OmarHayat, Thanks for your reply.Actually I want to update my location to the server for every 1 minute and I want to unregister broadcast register when a button click.I want to update my location for evey 1 minute even when the application foreground, background, closed. I want to unregister broadcast register when a button click – Naveen Sep 28 '17 at 05:31
  • okey then use foreground service which update your location and in that service use post dealy of 1 mint and do your update after 1 mint delay.You can unregistered your receiver on button click like this if(receiver!=null) { this.unregisterReceiver(receiver); } – Omar Hayat Sep 28 '17 at 05:40
  • Foreground service never stop even os can't stop that service. – Omar Hayat Sep 28 '17 at 05:41
  • Thanks OmarHayat, what about when application is Closed ?I have to send the location even when application is closed also. How to stop service in the existing code which I am using ? – Naveen Sep 28 '17 at 06:01
  • Please check this tutorial it may help you https://stackoverflow.com/questions/36473546/android-how-to-get-user-location-continuously-even-your-app-is-killed – Omar Hayat Sep 28 '17 at 06:03
  • You can use LocationListener interface implement this interface in your service and use its method to get updated location – Omar Hayat Sep 28 '17 at 06:06
  • The link which I am using having almost the same code but I am unable to stop the service and unregister receiver. – Naveen Sep 28 '17 at 06:08
  • stopService(new Intent(this,YourServiceName.class)); stop like this – Omar Hayat Sep 28 '17 at 06:10
  • Please put try catch send exception you get while stooping your service – Omar Hayat Sep 28 '17 at 06:15
  • @Override public void onDestroy() { super.onDestroy(); stopSelf(); } put this in your service as well – Omar Hayat Sep 28 '17 at 06:18
  • I have added onDestroy() method but it didn't worked – Naveen Sep 28 '17 at 06:29
  • please send exception while stooping your service – Omar Hayat Sep 28 '17 at 06:30
  • ? I/BufferQueueProducer: [eg.com.servicelocation.ServiceLocActivity](this:0xb7557428,id:1494,api:1,p:22551,c:231) queueBuffer: fps=3.94 dur=1014.60 max=961.67 min=15.64 09-28 12:07:12.918 231-231/? I/SurfaceFlinger: [Built-in Screen (type:0)] fps:3.975461,dur:1006.17,max:952.41,min:16.78 09-28 12:07:12.998 883-970/? D/PerfServiceManager: [PerfService] MESSAGE_TIMEOUT:103 – Naveen Sep 28 '17 at 06:40
  • Unregistered receiver first then remove location callback then stop service – Omar Hayat Sep 28 '17 at 06:40
  • How to remove Location Callback ?I am new to Android can you please help me. – Naveen Sep 28 '17 at 06:42
  • locationManager.removeUpdates(locationListener); – Omar Hayat Sep 28 '17 at 06:48
  • I have to call this from activity.Im confused how to call this, I have to pass LocationListener Object – Naveen Sep 28 '17 at 06:53
  • Create static method in your service and call that from activity to removeUpdates – Omar Hayat Sep 28 '17 at 06:55
  • Using your serviceName.yourmethod(); – Omar Hayat Sep 28 '17 at 06:56
  • This is method I not try yet but may work put method in service public static void stopService(){ stopSelf(); } and call like this from activity may it work yourServiceName.stopService(); – Omar Hayat Sep 28 '17 at 07:07
  • hey Sorry for delay I have some other work.In the same static method I have to call both locationManager.removeUpdates(locationListener); and stopSelf(); WIll this work ? locationlistener object showing error cannot resolve symbol – Naveen Sep 28 '17 at 07:43
  • public static void stopService(){ stopSelf(); } yourServiceName.stopService(); use this only don't try to remove location updates and check if works then okey else send me your code – Omar Hayat Sep 28 '17 at 07:57
  • stopSelf() is not taking as static inside static method in service – Naveen Sep 28 '17 at 09:26