Reputation: 23
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
Upvotes: 1
Views: 6164
Reputation: 855
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 :
`
public class GcmNetworkTask extends GcmTaskService {
@Override
public int onRunTask(TaskParams taskParams) {
restartYourServiceHere();
return 0;
}
}
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));
}
}
For finding location you can use Google fusedLocationAPI, Location manager (If you are not getting gps you can get location by network also).
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();
}
And inside onStartCommand method fetch location and do what ever you want.
Upvotes: 5