几个小时后,前台服务停止

时间:2019-11-22 13:53:08

标签: android unity3d service location

创建了Android服务,以在后台更新位置并将其与Unity集成。服务在启动时可以正常工作,但几个小时后服务停止,并且不会更新用户位置。

下面是 OnStartCommand,OnCreate 和Override方法的代码段。

public int onStartCommand(Intent intent, int flags, int startId)
{


    super.onStartCommand(intent, flags, startId);

    return START_STICKY;
}

public void onCreate()
{

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

        NotificationChannel   channel  = new NotificationChannel(
                "channel_01",
                "My Channel",
                NotificationManager.IMPORTANCE_DEFAULT
        );
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
        Notification.Builder builder = new Notification.Builder(getApplicationContext(), "channel_01");
        startForeground(12345678, builder.build());


    }
    initializeLocationManager();

    stopForeground(true);


}

任何帮助将不胜感激。谢谢

2 个答案:

答案 0 :(得分:0)

您是否尝试扩展JobService?即使重新启动手机,它对我也很好。

public class NotificationService extends JobService {


    public static final String TASK_ID = "notification_task_id";
    private static final String TAG = "NotificationJobService";
    private boolean jobCancelled = false;
    private String notificationTaskID;



    @Override
    public boolean onStartJob(JobParameters params) {
        Log.d(TAG, "onStartJob: Job Started");
        doBackgroundWork(params);
        return true;
    }

    private void doBackgroundWork(final JobParameters params) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                notifiy(params);
            }
        }).start();
    }

    public void notifiy(JobParameters params) { // dzieje się w przy notyfikacji


        createNotification("Title", "message", 'channelID', getApplicationContext());

        jobFinished(params, false);
    }


    public void createNotification(String aTitle, String aMessage, int channelID, Context context) {
        // create your notification here
    }

    @Override
    public boolean onStopJob(JobParameters params) {

        Log.d(TAG, "onStopJob: Job Cancelled");
        jobCancelled = true;
        return true;
    }


}

那是你怎么称呼它:

ComponentName componentName = new ComponentName(this, NotificationJobService.class);
        JobInfo info = new JobInfo.Builder(123, componentName)
                .setRequiresCharging(false)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .setPersisted(true)
                .setPeriodic(15*60*1000) // here you set the time
                .build();
        JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
        int resultCode = scheduler.schedule(info);
        if (resultCode == JobScheduler.RESULT_SUCCESS) {
            Log.e(TAG, "scheduleJob: Job scheduled");
        } else {
            Log.e(TAG, "scheduleJob: Job schedule failed");
        }

答案 1 :(得分:0)

如果您需要“永远”运行,则您的服务必须保持为前台。问题在于您正在停止前台。

尝试一下:

import android.app.Notification;
...
public void onCreate()
{
   super.onCreate();
   ...

   // Build the notification.
   Notification notification = notificationBuilder.build();

   // And Start Foreground.
   startForeground(SERVICE_ID, notification); // Where SERVICE_ID is any integer.
}

并且:

public int onStartCommand(Intent intent, int flags, int startId)
{
        //Return START_STICKY to inform the system that service must be woken up automatically if destroyed
        return START_STICKY;
}

注意:您可以参考此示例。 https://github.com/android/location-samples/tree/master/LocationUpdatesForegroundService

编辑:

如果您确实需要在后台(而不是在前台)进行维护,则可以参考Workers。此处更多信息:https://medium.com/androiddevelopers/workmanager-periodicity-ff35185ff006 请记住,后台定位的频率可能较低(每15分钟更新1次)。但是在前台,您可以使其更加频繁。