2014-11-04 56 views
0
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100),MyIntent,PendingIntent.FLAG_UPDATE_CURRENT); 
    //A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent 

    mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText,pendingIntent); 

    mNotification.flags |= Notification.FLAG_NO_CLEAR; 
    notificationManager.notify(0, mNotification); 

    Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_HOME); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    MainActivity.this.finish(); 
    startActivity(intent); 

即使設備重新啓動後,我仍需要將通知保存在通知欄中。即使在設備關閉並重新啓動後,如何在通知欄中使通知仍然存在?

+0

把你的完整代碼? – 2014-11-04 07:39:22

+0

這是完整的代碼。我只是想問一下設備重新啓動後是否有任何標誌設置可以讓通知在酒吧中持續存在? – 2014-11-05 06:03:39

回答

1

顯示通知,當設備重新啓動時,您可以使用以下方法。系統重新啓動時,通知紙盤變空。即使在系統關閉的情況下,也無法將這種通知留在內存中。顯示持久通知的唯一方法是將數據存儲在某些類似數據庫或共享首選項的位置,並在系統重新啓動時顯示相同的通知。

Android BroadcastReceiver on startup - keep running when Activity is in Background

只寫onRecieve方法內代碼將被稱爲當設備重新啓動。

首先您需要在操作名稱爲android.intent.action.BOOT_COMPLETED的清單中定義一個接收者。

<!-- Start the Service if applicable on boot --> 
<receiver android:name="com.prac.test.ServiceStarter"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
    </intent-filter> 
</receiver 

> 確保還包括完整的啓動權限。

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

    import android.content.BroadcastReceiver; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.util.Log; 

    public class ServiceStarter extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 

/* show notification here*/ 
     } 
    } 
+0

感謝您的回答。但是這不會讓通知持續存在。你可以在這裏開始新的通知,但不是之前的通知。 – 2014-12-18 12:06:43

相關問題