繁体   English   中英

从最近的应用列表中删除应用时,请避免取消通知

[英]Avoid cancelation of Notification on removing app from recent apps list

我正在使用以下代码段显示来自我的应用内的服务的通知:

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(currentNotificaion.getMessageTitle())
                .setContentIntent(contentIntent)
                .setContentText(currentNotificaion.getMessageText())
                .setAutoCancel(true);
int mNotificationId = (int) currentNotificaion.getMessageServerID();
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());

我的服务在清单中声明如下:

<service
android:name="com.myapp.services.NotificationService"
android:stopWithTask="false">
</service>

但是,当从最近的应用列表关闭我的应用时,通知将消失并从通知栏中删除。 另一件事是我不会使用从通知栏中删除的粘贴通知。

我怎么能避免这个?

在Manifest文件中,将service stopWithTask标记为true。 喜欢:

<service
    android:name="com.myapp.MyService"
    android:stopWithTask="true" />

我刚刚解决了类似的问题。

如果通过从最近的应用程序列表中轻扫应用程序而终止服务,那么您可以执行以下操作。

  1. 在Manifest文件中,将service stopWithTask标记为true。 喜欢:

但正如你所说,你想取消注册听众并停止通知等,我会建议这种方法:

Inside your Manifest file, keep flag stopWithTask as false for Service. Like:

    <service
        android:name="com.myapp.MyService"
        android:stopWithTask="false" />
  1. 现在在MyService服务中,覆盖onTaskRemoved方法。 (仅当stopWithTask设置为false时才会触发)。

     public void onTaskRemoved(Intent rootIntent) { //unregister listeners //do any other cleanup if required //stop service stopSelf(); } 

希望它会对你有所帮助。

如果您希望后台服务与前台通知一起运行,最好的方法是使用startForeground()函数。 您可以查看Android文档在这里 ,有关于它的一些答案在SO也很喜欢这个来自@CommonsWare与包含的例子。

希望能帮助到你!

如果您想在申请被杀后显示通知,我会建议您这样做。

正如@JamilHasnineTamim所写,当应用程序从设备中被杀死时,您可以捕获事件。

<service
        android:name="com.myapp.MyService"
        android:stopWithTask="false" />

内部服务

public void onTaskRemoved(Intent rootIntent) {
//your code
//stop service
    stopSelf();  
}

但是,在此代码中,您可以在几秒钟后向AlarmManager充电以重新启动您自己的通知。 把它放在onTaskRemoved

AlarmManager am = (AlarmManager) ctx.getSystemService(Activity.ALARM_SERVICE);
Intent intent = new Intent(BRNotificator.INTENT_FILTER);
int somealarmnumber = 10000;//or any numbe your want
long nexttime = System.currentTimeInMillis() + 5000L;
PendingIntent pi =PendingIntent.getBroadcast(ctx, somealarmnumber, intent, PendingIntent.FLAG_CANCEL_CURRENT):
am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nexttime, pi);

广播接收者是:

public class BRNotificator extends WakefulBroadcastReceiver {
    public static final String INTENT_FILTER = "com.example.BRNotificator";
    @Override
    public void onReceive(Context ctx, Intent intent) {
//restart your service this notification
        OWakeLocker.acquire(ctx, 0);
        ComponentName comp = new ComponentName(ctx.getPackageName(),
                YourServiceClass.class.getName());
        startWakefulService(ctx, intent.setComponent(comp));

//or just show your notification again:
NotificationCompat.Builder mBuilder = //... your code to show
    }

}

这是一个帮助唤醒你的设备的助手:

public class OWakeLocker {
    private static PowerManager.WakeLock[] wakeLocks = new PowerManager.WakeLock[1];//Services count

    @SuppressWarnings("deprecation")
    public static void acquire(Context ctx, int index) {
        WakeLock wakeLock = wakeLocks[index];
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                    PowerManager.ACQUIRE_CAUSES_WAKEUP |
                    PowerManager.ON_AFTER_RELEASE, _.APPNAME + Integer.toString(index));
        if (wakeLock != null && wakeLock.isHeld()){
            wakeLock.acquire();
        }
    }

    public static void release(int index) {
        WakeLock wakeLock = wakeLocks[index];
        if (wakeLock != null) 
            wakeLock.release();
            wakeLock = null;
    }
}

你甚至可以

public static void clearNotifications(Context context) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancelAll();
}
 .setAutoCancel(true);

我们将其设置为false,因此通知不会被取消而不会被删除。 你也可以

.setOngoing(true)

更新:如果以上不起作用,并且从服务发送通知,则需要致电

 startForeground(int, Notification) 

为了在app被杀死时不杀死服务。

来自Android文档:

已启动的服务可以使用startForeground(int,Notification)API将服务置于前台状态,系统将其视为用户主动了解的内容,因此在内存不足时不会成为查杀的候选者。

可以在这里找到更多: Android Service startForeground

暂无
暂无

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

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