首页 > 解决方案 > 如何制作手机屏幕关闭时不停止的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.android&hl=en 这个应用程序不需要任何特殊权限并跟踪路径,我得到一个线索这种服务是如何运作的?

标签: javaandroidforeground-service

解决方案


推荐阅读