【问题标题】:How to restart sticky service after power saver kills it on android节电程序在android上杀死它后如何重新启动粘性服务
【发布时间】:2017-04-28 05:47:34
【问题描述】:

我尝试设置android:isolatedProcess="true",但它不起作用

其实我想一直显示永久通知

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)

.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), resourceIcon/*R.mipmap.ic_launcher*/))
                .setSmallIcon(R.drawable.ic_icon_flo_not)
                .setContentTitle(title)
                .setOngoing(onGoing)
                .setAutoCancel(autoCancelable)
                .setPriority(priority)
                .setContentText(message);
        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (playSound)
            mBuilder.setSound(soundUri);
        if (remoteViews == null) {
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(context, resultClass);
            resultIntent.setAction(action);

            // The stack builder object will contain an artificial back stack for the
            // started Activity.
            // This ensures that navigating backward from the Activity leads out of
            // your application to the Home screen.
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(MainActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
        } else {
            mBuilder.setContent(remoteViews);
        }

【问题讨论】:

  • 您需要从您的应用程序再次启动服务。
  • @ChetanJoshi 我怎么能这样,因为省电/省电模式会杀死我的应用程序。

标签: android service process


【解决方案1】:

在您的服务中覆盖 onTaskRemoved() 并使用警报管理器再次启动服务。下面是代码。

    @Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);

    Log.d(TAG, "TASK REMOVED");

    PendingIntent service = PendingIntent.getService(
            getApplicationContext(),
            1001,
            new Intent(getApplicationContext(), MyService.class),
            PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
}  

【讨论】:

  • 已经做了这件事,但启动节电/节电后无法正常工作
  • 在省电方面,Android OS 强行关闭了许多您对此无能为力的进程,甚至关闭了部分进程
  • 很少有应用程序处理这个问题。我的数据管理员play.google.com/store/apps/…
  • @GOURAVMEHTA 上面提到的App(Data Manager)只有在开启超级省电模式前开启数据的情况下才会处理这个问题,否则关闭超级电源后无法重启服务保存模式。
【解决方案2】:

Service.START_STICKY 服务是那些由于某种原因被终止后重新启动的服务。您只需要从 onStartCommand 中返回 Service.START_STICKY

public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; }

你只需要在上面做,你不需要做单独的事情

【讨论】:

  • 你能分享代码sn-p吗?你在服务中所做的可能是它不断重启
【解决方案3】:

服务确实重新创建,不重新启动。 如果您覆盖 onCreate 并执行 Log.d 或 Toast,您将看到 onCreate 在您的活动和应用程序被销毁后被调用。

因此,在重新创建后保持其运行的诀窍是在 onCreate 方法上执行您的代码,然后将 onStartCommand 用于 return START_STICKY。


另一件需要注意的事情(由于我无法发表评论,我想用AlarmManager 补充 Abdul Kawee 的答案): 它已在 api27 上得到解决:您所要做的就是将您的 super.onTaskRemoved(rootIntent) 移动到代码的最后一行。

我发现破坏性方法只能在被覆盖的方法结束时调用super,否则运行时部分资源不再可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多