GPS跟踪系统?



我必须创建一个服务来跟踪用户的GPS位置,即使应用程序被杀死,直到我停止服务。可能吗? 我创建了一个使用LocationListener的服务,并且还利用了与服务器通信的 socket.io。但是当应用程序暂停时,位置更新不会反映,当我杀死应用程序时,套接字会关闭。请提出适当的实施方式。

链接到我创建的服务

>这是我的onStartCommand

h = new Handler();
r = new Runnable() {
@Override
public void run() {
startSomeJob();
startForeground(101, updateNotification(0));
}
};
h.post(r);

这是通知:

private Notification updateNotification(int progress) {
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(action), 0);
String info = progress + "%";
NotificationManager mNotifyManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) if (mNotifyManager != null) {
createChannel(mNotifyManager);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "101")
.setContentTitle(info)
.setTicker(info)
.setContentText("Uploading")
.setVibrate(new long[] {0L})
.setProgress(100, progress, false)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setOngoing(true);
return builder.build();
}

在我的某个工作中,如果有一些更新,它会调用startForeground(101, updateNotification(percentDone));

在函数onLocationChanged中位置更改的情况下,您需要调用 startForeground 并发出通知。

最新更新