Java 如何使GPS前台服务在手机屏幕关闭时不停止?

Java 如何使GPS前台服务在手机屏幕关闭时不停止?,java,android,foreground-service,Java,Android,Foreground Service,我正在尝试创建一个前台服务来跟踪用户的运行路径 我想这个服务器运行和跟踪用户的位置,直到用户关闭,但现在它的工作与位置跟踪和路径良好,但唯一的问题是,当我的服务,把手机在口袋里,然后在一段时间后,服务被破坏。但我把它放在桌子上,然后服务通宵运行 以下是启动该程序的代码 javastartforegroundservice(新意图(这个,GoogleService.class)) 以下是启动服务时启动的计时器代码 mTimer.schedule(new TimerTaskToGet

我正在尝试创建一个前台服务来跟踪用户的运行路径

我想这个服务器运行和跟踪用户的位置,直到用户关闭,但现在它的工作与位置跟踪和路径良好,但唯一的问题是,当我的服务,把手机在口袋里,然后在一段时间后,服务被破坏。但我把它放在桌子上,然后服务通宵运行

以下是启动该程序的代码
javastartforegroundservice(新意图(这个,GoogleService.class))

以下是启动服务时启动的计时器代码

        mTimer.schedule(new TimerTaskToGetLocation(), 10,1000 );```

in TimerTasktoGetLocation 
```java                         sendNotification_pause(getString(R.string.app_name)+" mp service",utils.convertSecondsToHMmSs(total_time),distance);
这是显示通知的代码

RemoteViews notificationLayout=null;
notificationLayout=新的远程视图(getPackageName(),R.layout.custom\u通知\u位置);
Intent Intent=newintent(getApplicationContext(),GoogleService.class);
PendingEvent PendingEvent PendIntentTapp_oopen=PendingEvent.getActivity(this,0,新意图(context,DrawerActivity.class)).setFlags(意图.FLAG_活动_重新排序_至_前端),0);
PendingIntent pending_intent_end=PendingIntent.getService(this,0,intent.setAction(Actions.SERVICE_STOP),0);
notificationLayout.setTextViewText(R.id.custom_notification_Distanz:+Utils.distance_与_unite(distance))的距离);
notificationLayout.setTextViewText(R.id.custom\u notification\u duration,“Dauer:+duration”);
NotificationCompat.Builder=新建NotificationCompat.Builder(此“CANNEL01”)
.setSmallIcon(R.drawable.login_徽标)
.setAutoCancel(错误)
.setVisibility(通知兼容性可见性\u公共)
.setPriority(通知管理器重要性高)
.setStyle(新通知Compat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setonlylertonce(真)
.addAction(新的通知兼容操作(R.drawable.ic\u close\u black\u 24dp,“End”,pending\u intent\u End))
.setContentIntent(pendIntentapp_oopen);
builder.setcongressing(true);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
int重要性=NotificationManager.importance\u HIGH;
@SuppressLint(“错误常量”)NotificationChannel=新NotificationChannel(“CANNEL01”,“位置”,重要性);
channel.setDescription(“所有不同的间隔音”);
//向系统注册频道;您不能更改其重要性
//或在此之后的其他通知行为
NotificationManager NotificationManager=getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(频道);
startForeground(101,builder.build());
}否则{
startForeground(101,builder.build());
NotificationManagerCompat notificationManager=NotificationManagerCompat.from(此);
}
我想这个服务器运行和跟踪用户的位置,直到用户关闭,但现在它的工作与位置跟踪和路径良好,但唯一的问题是,当我的服务,把手机在口袋里,然后在一段时间后,服务被破坏。还有一件事,当我把它放在桌子上时,服务会整夜运行。 我想知道为什么会这样

例如
此应用程序不需要任何特殊许可并跟踪路径,如果我能了解此类服务的工作原理,这将是一个很好的方法?

感谢您的评论,我已经测试过这种方法,但当我启动服务并将手机放入口袋并开始驾驶时,它会终止服务,因此我看到的上述问题中没有解释这一点;你有一个工作的前台服务。我想知道,你是如何确定服务正在被破坏的?通知消失了?另外,当它运行一整晚时,它是否正在收费?是的,通知是“走开”,我想告诉你的是,我正在清除服务onDestroy功能中的所有通知,我注意到一件事,通知更新停止,因为当我解锁手机通知时,它不会更新,但在一些时间之后它会再次开始更新,我使用计时器持续更新通知,显示从服务开始到充电的持续时间?