有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

以下是启动该程序的代码 java startForegroundService(new Intent(this, 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= new RemoteViews(getPackageName(), R.layout.custom_notification_location);


        Intent intent = new Intent(getApplicationContext(), GoogleService.class);
        PendingIntent pendIntentapp_oopen = PendingIntent.getActivity(this, 0, new Intent(context, DrawerActivity.class).setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0);
        PendingIntent pending_intent_end = PendingIntent.getService(this, 0, intent.setAction(Actions.SERVICE_STOP), 0);

       notificationLayout.setTextViewText(R.id.custom_notification_distance, "Distanz: "+Utils.distance_with_unite(distance));
        notificationLayout.setTextViewText(R.id.custom_notification_duration, "Dauer: "+duration);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "CANNEL01")
                .setSmallIcon(R.drawable.login_logo)
                .setAutoCancel(false)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setPriority(NotificationManager.IMPORTANCE_HIGH)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setCustomContentView(notificationLayout)
                .setOnlyAlertOnce(true)
                .addAction(new NotificationCompat.Action(R.drawable.ic_close_black_24dp,"End",pending_intent_end))
                .setContentIntent(pendIntentapp_oopen);


        builder.setOngoing(true);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            int importance = NotificationManager.IMPORTANCE_HIGH;
            @SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel("CANNEL01", "Location", importance);
            channel.setDescription("All the different interval tones");
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
            startForeground(101,builder.build());


        }else {
            startForeground(101,builder.build());

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        }

我想这个服务器运行和跟踪用户的位置,直到用户关闭,但现在它的工作与位置跟踪和路径良好,但唯一的问题是,当我的服务,把手机在口袋里,然后在一段时间后,服务被破坏。还有一件事,当我把它放在桌子上时,服务会整夜运行。 我想知道为什么会这样

例如https://play.google.com/store/apps/details?id=com.runtastic.安卓&hl=en 这个应用程序不需要任何特殊的权限和跟踪路径,这也将是一个很好的我得到这种服务如何工作的线索


共 (0) 个答案