android - 如何在 android kitkat 中保持服务活跃?

标签 android android-4.4-kitkat

你好, friend 们,我已经搜索了很多,但没有找到 Google 所做的服务更改,我需要让我的服务保持活力。

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

    return START_STICKY;
    } 

但它没有在 android 4.4.2 中重新启动服务对此有什么想法吗?

最佳答案

您可以将它用于 Android kitkat,因为它们正在从后台清除进程。

 @Override
        public void onTaskRemoved(Intent rootIntent) {
         Intent restartService = new Intent(getApplicationContext(),
                    this.getClass());
            restartService.setPackage(getPackageName());
            PendingIntent restartServicePI = PendingIntent.getService(
                    getApplicationContext(), 1, restartService,
                    PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmService = (AlarmManager) getApplicationContext()
                    .getSystemService(Context.ALARM_SERVICE);
            alarmService.set(AlarmManager.ELAPSED_REALTIME,
                    SystemClock.elapsedRealtime() + 5000, restartServicePI);

        }

关于android - 如何在 android kitkat 中保持服务活跃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24075740/

相关文章:

Android:如何在位置访问取景器(从适配器外部)

android - 在 KitKat 中获取短信访问权限

android - Flexbox 在 Android 4.4.2 的 Android Web 浏览器中不工作

android - WebView 缓存在 Android 4.4 中不起作用

android - Android 4.4 KitKat build 中位于/system/app 的系统应用如何获得系统权限?

java - Android:多个 Activity 的抽屉导航

java - Android后台HTTP请求不起作用?

android - Cloud Firestore API 是否使用某种网络安全协议(protocol)?

android - FLAG_TRANSLUCENT_NAVIGATION 在横向模式下不可用?

Android WebView : handling orientation changes