簡體   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