[英]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. 我不想使用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. 我尝试使用Service,IntentService,Foreground服务,Socket线程等,但是当我关闭主要活动时,它有时在后台运行,然后被杀死。
How can I do this? 我怎样才能做到这一点? Please I beg you, help me <3
请求求你,帮我<3
Yeah Let me Tell you, If you are having devices like Xiaomi, oppo, Vivo. 是的,让我告诉您,如果您拥有小米,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. AlarmManager重新启动您的服务。
Bootcomplete receiver is used to start your services when system reboots Bootcomplete接收器用于在系统重新引导时启动服务
public class BootCompleteReceiver extends BroadcastReceiver { 公共类BootCompleteReceiver扩展了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 : 然后服务的onstartCommand中的警报管理器将执行以下操作:
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). 要查找位置,您可以使用Google fusedLocationAPI,位置管理器(如果您没有获取GPS,也可以通过网络获取位置)。
If anyone finding it too tricky, you can go with a hack : in OnDestroy of service restart your service as : 如果有人发现它太棘手,则可以采取以下措施:在服务的OnDestroy中,按以下方式重新启动服务:
@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. @Override public void onDestroy() { Intent startSeviceIntent = new Intent(getApplicationContext(), SampleService.class); startService(startSeviceIntent); super.onDestroy(); }
而onStartCommand方法内获取位置和你什么都想要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.