2013-05-31 19 views
6

我想讓我的Android通知逗留即使用戶點擊或單擊清除所有...如何使通知uncancellable /不可移動

現在它停留有時,有時被刪除,我不確定是什麼導致它。

這裏是我的通知代碼:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
public static void createNotification() 
{ 
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
          .setContentTitle("Wolftech Field Agent") 
          .setContentText("Getting Status") 
          .setSmallIcon(R.drawable.ic_launcher) 
          .setOngoing(true) 
          .setAutoCancel(false); 

    Intent intent = new Intent(context, FieldAgent.class); 
    intent.setAction(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    stackBuilder.addParentStack(FieldAgent.class); 
    stackBuilder.addNextIntent(intent); 

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

    builder.setContentIntent(pendingIntent); 

    notificationManager.notify(NOTIFICATION_ID, builder.build()); 
} 

public static void updateNotificationText(String inString) 
{ 
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
              .setContentText(inString) 
              .setContentTitle("Wolftech Field Agent") 
              .setSmallIcon(R.drawable.ic_launcher) 
              .setOngoing(true) 
              .setAutoCancel(false); 

    Intent intent = new Intent(context, FieldAgent.class); 
    intent.setAction(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    stackBuilder.addParentStack(FieldAgent.class); 
    stackBuilder.addNextIntent(intent); 

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

    builder.setContentIntent(pendingIntent); 

    notificationManager.notify(NOTIFICATION_ID, builder.build()); 
} 

public static void cancelNotification() 
{ 
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.cancel(NOTIFICATION_ID); 
} 

回答

1

Okey,我意識到我發佈的代碼其實很好。

發生了什麼事情是,當我點擊通知時,它再次調用onCreate(),然後在隨機間隔後調用onDestroy()。我有我的cancelNotification()方法,所以如果onDestroy()在onCreate()後被調用,它會刪除我的通知。

這給我帶來了一個新問題:爲什麼在我遵循我在這裏找到的關於如何阻止這種情況發生的每個答案之後,它會破壞並重新創建我的活動?

如果你想幫助我解決這個問題,您可以在這裏找到這個問題How to make notification resume and not recreate activity? ......

1

您嘗試

PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
notificationManager.cancel(pendingIntent); 
+0

我想你誤解了這個問題(或者我誤解了你的回答......)我不想取消通知(當然,我這樣做,當我決定時),我想讓通知保持每個計時器用戶點擊它或點擊「全部清除」。 – CeeRo

4

我相信你正在尋找這個標誌:

Notification.FLAG_NO_CLEAR 

添加一個標誌,以您的通知。

+0

當我點擊全部清除時,它似乎仍然保留,但當它點擊時它仍然會消失。奇怪 – CeeRo

+0

現在我傾向於重新創建/更新onResume()上的通知,以便在每次點擊後重新制作,似乎沒有辦法確保它真的保持其他方式... – CeeRo

+0

我想通了是什麼導致它,檢查我的回答 – CeeRo

8

我認爲你正在尋找正在進行的通知,在這種情況下,如果你使用的是NotificationCompat.Builder,你可以使用:

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this); 
mNotifyBuilder.setOngoing(true); 
+0

這是否必須是前臺服務? –

-1

我也有這個問題。我的解決方案是創建意圖

 private void addNotification(String headLine, String info, Context context) { 
     mBuilder = 
       new NotificationCompat.Builder(context) 
         .setSmallIcon(R.drawable.icon) //R.drawable.icon 
         .setContentTitle(headLine) 
         .setContentText(info) 
         .setOngoing(true) 
         .setAutoCancel(false); 
// Creates an explicit intent for an Activity in your app 
     Intent resultIntent = new Intent(context, MainActivity.class); 
     resultIntent.putExtra("FROM_NOTIF", true); 
// 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); 

     mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
// mId allows you to update the notification later on. 
     //  mBuilder.setf |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; 
     mNotificationManager.notify(mNotification_id, mBuilder.build()); 
    } 

,並在MainActivity功能的添加額外的:

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    if (intent.getBooleanExtra("FROM_NOTIF", false)) { 
     addNotification(...) 
      ... call the function that adds the notification again ..} 

這樣的事情是,通知它用戶按下後關閉,但比你能通知使用onNewIntent()開始的活動,檢查那裏的意圖,如果發現它來自通知,請重新設置它。

+0

雖然此代碼可能會回答問題,但提供有關如何解決問題和/或爲何解決問題的其他上下文將提高​​答案的長期價值。請閱讀[如何回答](http:// stackoverflow。com/help /如何回答)以提供高質量的答案。 – thewaywewere

+0

編輯我的答案 –