简体   繁体   English

服务在 android 8 (oreo) 下意外停止

[英]service stops unexpectedly under android 8 (oreo)

I have written and published an app ("Sensor Recording") in the Google Play Store.我在 Google Play 商店编写并发布了一个应用程序(“传感器录音”)。 It's about reading sensor data (such as position, accelerometer, gyroscopes, etc.), displaying them numerically or graphically and storing them into kml and csv files for further processing, eg with Google Earth or MS Excel.它是关于读取传感器数据(例如位置、加速度计、陀螺仪等),以数字或图形方式显示它们并将它们存储到 kml 和 csv 文件中以供进一步处理,例如使用 Google Earth 或 MS Excel。 I have established a service to read and process the data in the background even when the screen is switched OFF.我已经建立了一项服务,即使屏幕关闭,也可以在后台读取和处理数据。

Everything was working fine until Android 8. But in Oreo, the service is stopped automatically by the operating system, approx.一切正常,直到 Android 8。但在 Oreo 中,操作系统会自动停止该服务,大约。 5 minutes after the screen is switched OFF.屏幕关闭后 5 分钟。 This has been introduced by Google intentionally to save battery lifetime.这是谷歌有意引入的,以节省电池寿命。 I have found some measures in the internet which should avoid that, but nothing worked so far.我在互联网上找到了一些应该避免这种情况的措施,但到目前为止没有任何效果。

What I have done:我做了什么:

1.) in the calling activity I have replaced startService() with startForegroundService() 1.) 在调用活动中,我用startForegroundService()替换了startService() startForegroundService()

2.) in the service itself I have made some modifications in onStartCommand() according to the hints I have found. 2.) 在服务本身我根据我发现的提示对onStartCommand()做了一些修改。

Tests with wakeLock also led to nothing.使用wakeLock测试也没有任何结果。 Any further ideas are appreciated.任何进一步的想法表示赞赏。

private NotificationManager notMan;

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    // show notification + startForeground

    int id = 42;
    String channelId = "42";
    String text = "Sensors active";

    Intent notificationIntent = new Intent(this, SensorService.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    Notification notification;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
    {
        NotificationChannel channel = new NotificationChannel(channelId,
                getString(R.string.app_name),
                NotificationManager.IMPORTANCE_DEFAULT);

        notMan = getSystemService(NotificationManager.class);

        notMan.createNotificationChannel(channel);

        Notification.Builder builder = new Notification.Builder(this, channelId);

        builder.setSmallIcon(R.drawable.vector3d_bg_transp)
                .setContentTitle("Sensor Service")
                .setContentText(text)
                .setTicker(text)
                .setSubText("Start Service")
                .setShowWhen(true);

        notification = builder.build();
    }
    else
    {
        notMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.vector3d_bg_transp)
                .setContentTitle("Sensor Service")
                .setContentText(text)
                .setTicker(text)
                .setSubText("Start Service")
                .setPriority(PRIORITY_DEFAULT)
                .setShowWhen(true).build();
    }

    startForeground(id, notification);

    return super.onStartCommand(intent, flags, startId);    // = 1, same as START_STICKY
}  // onStartCommand

Recently, I found the solution – after an essential hint by Thomas Künneth (author of various Android books).最近,在 Thomas Künneth(各种 Android 书籍的作者)的重要提示之后,我找到了解决方案。 The remedy is not in the source code, but in the settings of the smartphone.补救措施不在源代码中,而在智能手机的设置中。 There is an option to enable background processing.有一个选项可以启用后台处理。 On my Huawei P10 Lite with Android 8.0 it is located in the following menu tree (probably other devices or android versions have similar options):在我的搭载 Android 8.0 的华为 P10 Lite 上,它位于以下菜单树中(可能其他设备或 Android 版本也有类似的选项):

  • Settings设置
  • Battery电池
  • Launch发射
  • Select the app in question.选择有问题的应用程序。
  • Set the switch from “Manage automatically” to “Manage manually”.将开关从“自动管理”设置为“手动管理”。
  • In the pop-up menu set the switch “Run in background”.在弹出菜单中设置开关“在后台运行”。

That's it, quite easy – if you know how.就是这样,很简单——如果你知道怎么做的话。

It is remarkable, that Google offers this option, but does not highlight it in lectures about Android 8. Of course, this is consistent with the new policy “Battery first” .值得注意的是,谷歌提供了这个选项,但在关于Android 8的讲座中并没有强调它。当然,这与“电池优先”的新政策是一致的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM