2012-04-14 64 views
2

我正在嘗試開發一個應用程序來組織。所以在設置特定時間的任務之後,我創建了一個通知,所以此通知顯示在狀態欄上,當用戶觸摸此通知時,將顯示當前活動。
我的問題是:當通知出現時,活動也啓動。
反正有防止呢?我的意思是,當通知出現時,活動不會自動啓動,用戶必須單擊通知才能喚醒活動。
只顯示通知而不開始活動

我有班級「主」和一個報警管理器裏面。此警報管理器將在特定時間觸發並開始另一項活動(顯示通知)。

{ 
    Intent intent = new Intent("com.test.DisplayNotification"); 
    intent.putExtra("alarm_message", "Alarm"); 
    intent.putExtra("item_name", "message"); 
    PendingIntent broadcast = PendingIntent.getActivity(getBaseContext(), 0, intent, 0); 
    AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); 
    am.set(AlarmManager.RTC_WAKEUP, mili, broadcast); 
} 

在DisplayNotification類我有

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     String message = getIntent().getExtras().getString("item_name"); 

     Intent i = new Intent("com.test.Main"); 


     PendingIntent detailsIntent = 
      PendingIntent.getActivity(this, 0, i, 0); 

     NotificationManager nm = (NotificationManager) 
      getSystemService(NOTIFICATION_SERVICE); 
     Notification notif = new Notification(
      R.drawable.icon, 
      "Time's up!", 
      System.currentTimeMillis()); 

     CharSequence from = "AlarmManager - Time's up!"; 
     notif.setLatestEventInfo(this, from, message, detailsIntent); 


     nm.notify(1, notif); 
     //---destroy the activity--- 
     finish(); 

    } 

回答

2

使用BroadcastReceiver爲「聽」的報警的結果,然後把它在其onReceive(...)方法創建Notification

+0

我剛纔想到了這個,但是我在獲取上下文時遇到了一些問題,但現在沒問題,謝謝帶我離開泥潭 – 2012-04-14 12:57:15

+0

@TrầnĐứcAnh:很高興我能幫上忙。 – Squonk 2012-04-14 12:59:39