简体   繁体   中英

Android Foreground Service For Location

I need to make an app (spy app) that runs in background everytime and send location of the device to a server.

I don't want to use Google Api.

I tried to use Service, IntentService, Foreground service, Thread for socket etc etc, but when I close main activity it runs in background for sometimes then get killed.

How can I do this? Please I beg you, help me <3

Yeah Let me Tell you, If you are having devices like Xiaomi, oppo, Vivo. You services will get killed by them. So no matter how hard you will try, it will be killed if you have not checked Autostart in settings. So you have to bound user to check it to make your app running in background.

So, there are many things you can do :

  1. Restart your services in receiver of GCM service by sending push notification from server to the users in every given interval of time.

`

public class GcmNetworkTask extends GcmTaskService {

        @Override
        public int onRunTask(TaskParams taskParams) {
            restartYourServiceHere();
            return 0;
        }
    }
  1. AlarmManager to restart your services.

    Bootcomplete receiver is used to start your services when system reboots

    public class BootCompleteReceiver extends BroadcastReceiver {

     @Override public void onReceive(Context context, Intent intent) { Intent service = new Intent(context, Sample_service.class); context.startService(service); } 

    }

then alarm manager in onstartCommand of service will do :

 intent = new Intent(this, ReceiverClass.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            this.getApplicationContext(), 234324243, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (40000), pendingIntent);
    return START_STICKY;

And then again a receiver class to catch even of alarm manager :

   public class ReceiverClass extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
      context.startService(new Intent(context, Sample_service.class));

    }
}
  1. Runnable thread will work if the activity gets paused but will stop if user destroys the activity.

For finding location you can use Google fusedLocationAPI, Location manager (If you are not getting gps you can get location by network also).

  1. If anyone finding it too tricky, you can go with a hack : in OnDestroy of service restart your service as :

    @Override public void onDestroy() { Intent startSeviceIntent = new Intent(getApplicationContext(), SampleService.class); startService(startSeviceIntent); super.onDestroy(); } @Override public void onDestroy() { Intent startSeviceIntent = new Intent(getApplicationContext(), SampleService.class); startService(startSeviceIntent); super.onDestroy(); } And inside onStartCommand method fetch location and do what ever you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM