Android 位置的前台服务

Android Foreground Service For Location

我需要制作一个每次都在后台运行的应用程序(间谍应用程序),并将设备的位置发送到服务器。

我不想使用 Google Api。

我尝试使用服务、IntentService、前台服务、套接字线程等等,但是当我关闭主程序时 activity 它有时会在后台运行然后被杀死。

我该怎么做? 求求你,帮帮我 <3

是的,让我告诉你,如果你有像小米、oppo、vivo 这样的设备。你的服务会被他们杀死。所以不管你怎么努力,如果你没有在设置中勾选自动启动,它就会被杀死。所以你必须绑定用户检查它才能让你的应用程序 运行 在后台。

因此,您可以做很多事情:

  1. 通过在每个给定的时间间隔从服务器向用户发送推送通知,在 GCM 服务的接收者中重新启动您的服务。

`

public class GcmNetworkTask extends GcmTaskService {

        @Override
        public int onRunTask(TaskParams taskParams) {
            restartYourServiceHere();
            return 0;
        }
    }
  1. AlarmManager 以重新启动您的服务。

    Bootcomplete 接收器用于在系统重新启动时启动您的服务

    public class BootCompleteReceiver 扩展 BroadcastReceiver {

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

    }

然后服务的 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;

然后又是一个接收器 class 来捕捉警报管理器的事件:

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

    }
}
  1. 如果 activity 被暂停,可运行线程将工作,但如果用户销毁 activity。
  2. 将停止

要查找位置,您可以使用 Google fusedLocationAPI,位置管理器(如果您没有获取 gps,您也可以通过网络获取位置)。

  1. 如果有人觉得它太棘手,你可以尝试一下:在服务的 OnDestroy 中重新启动你的服务:

    @Override public void onDestroy() { Intent startSeviceIntent = new Intent(getApplicationContext(), SampleService.class); startService(startSeviceIntent); super.onDestroy(); } 在 onStartCommand 方法中获取位置并做任何你想做的事。