【问题标题】:ForegroundService on android Oreo gets killedandroid Oreo 上的前台服务被杀死
【发布时间】:2018-10-09 13:25:34
【问题描述】:

我正在尝试建立一个每分钟请求设备位置的服务。

即使应用程序关闭,我也需要它在后台工作。到目前为止,我设法让它在具有 pre-Oreo android 操作系统的设备上运行,但现在我正在 android Oreo 设备上测试该服务,当我关闭或将应用程序置于后台时无法正常工作。

在我的研究中,我发现对于 Oreo 设备,应该使用带有持续通知的前台服务来实现这一点,因此我首先实现了一个简单的前台服务,如下所示,它在应用程序运行时显示持续通知启动并在应用停止时删除。

public class MyForegroundService extends Service {

    private static String TAG = MyForegroundService.class.getSimpleName();

    private static final String CHANNEL_ID = "channel_01";
    private static final int NOTIFICATION_ID = 12345678;

    private NotificationManager mNotificationManager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public MyForegroundService() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Android O requires a Notification Channel.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.app_name);

            // Create the channel for the notification
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);

            // Set the Notification Channel for the Notification Manager.
            mNotificationManager.createNotificationChannel(mChannel);
        }

        startForeground(NOTIFICATION_ID, getNotification());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");

        stopForeground(true);
    }

    private Notification getNotification() {

        // Get the application name from the Settings
        String appName = PrefApp.getSettings(getApplicationContext()).getAppConfigs().getAppName();
        String applicationKey = PrefApp.getSettings(getApplicationContext()).getAppConfigs().getAppKey();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setContentTitle(appName)
                .setContentText("Services are running")
                .setOngoing(true)
                .setPriority(Notification.PRIORITY_HIGH)
                .setSmallIcon(R.mipmap.ic_notification)
                .setWhen(System.currentTimeMillis());

        // Set the Channel ID for Android O.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(CHANNEL_ID); // Channel ID
        }

        return builder.build();
    }
}

我正在使用以下函数启动和停止上述服务。

public void startMyForegroundService() {
    Log.d(TAG, "Start Foreground Service");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(new Intent(getApplicationContext(), MyForegroundService.class));
    } else {
        startService(new Intent(getApplicationContext(), MyForegroundService.class));
    }
}

public void stopMyForegroundService() {
    Log.d(TAG, "Stop Foreground Service");
    stopService(new Intent(getApplicationContext(), MyForegroundService.class));
}

我正在测试上述服务,由于某种原因,该服务在我启动后大约 30 分钟后被终止。谁能告诉我我做错了什么或可能指导我找到适合我的解决方案?

注意:我已经关注了这个tutorial 并测试了他们的应用程序,但它仍然无法正常工作。该服务在一段时间后被终止。

基本上我的目标是实现一个可以在后台运行的服务(即使应用程序关闭)并每分钟获取一次位置更新。

【问题讨论】:

  • 只有在您不使用设备 30 分钟时才会被杀死吗?可能是 Doze 干扰您的服务,并导致意外崩溃。从应用程序死亡时是否有相关的 logcat?
  • 设备将被插入,并且根据我在文档中阅读的内容,如果设备正在充电,则不应进入打盹模式。除此之外,我一直在监控它,所以我正在使用该设备。不幸的是,当服务被终止时,logcat 会自动清除,所以我看不到任何日志。
  • "...logcat 会自动清除..." 我会先解决这个问题。 logcat 可以帮助您了解发生了什么,而不是在您最需要的时候破坏信息。考虑调整 Android Studio logcat 窗格上的过滤器,以便您可以查看所有日志。

标签: android location android-8.0-oreo android-8.1-oreo


【解决方案1】:

您可以将 Firebase 作业调度程序用于后台服务。

代码: 添加此依赖项:

implementation 'com.firebase:firebase-jobdispatcher:0.8.5'

public class MyJobService extends JobService
{
    private static final String TAG = MyJobService.class.getSimpleName();

    @Override
    public boolean onStartJob(JobParameters job)
    {
        Log.e(TAG, "onStartJob: my job service class is called.");
        // enter the task you want to perform.
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters job)
    {
        return false;
    }
}

在 Activity 中创建一个作业,你可以像以前为后台服务那样做。

/**
 * 2018 September 27 - Thursday - 06:36 PM
 * create job method
 *
 * this method will create job
**/
private static Job createJob(FirebaseJobDispatcher dispatcher)
{
    return dispatcher.newJobBuilder()
            //persist the task across boots
            .setLifetime(Lifetime.FOREVER)
            //.setLifetime(Lifetime.UNTIL_NEXT_BOOT)
            //call this service when the criteria are met.
            .setService(MyJobService.class)
            //unique id of the task
            .setTag("TAGOFTHEJOB")
            //don't overwrite an existing job with the same tag
            .setReplaceCurrent(false)
            // We are mentioning that the job is periodic.
            .setRecurring(true)
            // Run every 30 min from now. You can modify it to your use.
            .setTrigger(Trigger.executionWindow(1800, 1800))
            // retry with exponential backoff
            .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
            //.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
            //Run this job only when the network is available.
            .setConstraints(Constraint.ON_ANY_NETWORK)
            .build();
}

/**
 * 2018 September 27 - Thursday - 06:42 PM
 * cancel job method
 *
 * this method will cancel the job USE THIS WHEN YOU DON'T WANT TO USE THE SERVICE ANYMORE.
**/
private void cancelJob(Context context)
{
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
    //Cancel all the jobs for this package
    dispatcher.cancelAll();
    // Cancel the job for this tag
    dispatcher.cancel("TAGOFTHEJOB");
}

【讨论】:

  • 这是否也会在任何SDK的Android手机空闲时触发?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
相关资源
最近更新 更多