2013-01-13 22 views
1

我創建了一個通過AlarmManager啓動的簡單狀態通知。一切工作正常(通知的內容,標題,點擊時啓動的活動等)。不幸的是,當通知被調用(即AlarmManager關閉)時,會啓動並顯示一個空的活動。該活動只是在狀態欄中有我的應用程序名稱和圖標。實際活動本身是空白的。同樣,當通知關閉並且第一次出現在狀態欄中時,會發生這種情況。當我再次點擊通知時,它會轉到正確的待處理活動。這裏是我的代碼:NotificationManager啓動空活動

下面是設置AlarmManager打電話通知代碼:

//Use AlarmManager to trigger the notification/alarm. 
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

//PendingIntent to launch activity when the alarm triggers.      
Intent intent = new Intent("com.YouForgotWhat.FlightGear.DisplayNotification"); 

PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, 0); 

//Set an alarm to go off at 30 minutes before fuel runs out. 
alarmManager.set(AlarmManager.RTC_WAKEUP, endTimeInMillis - (30*60*1000), displayIntent); 

,這裏是通知本身(這是在其他活動)的實際代碼:

公共類DisplayNotification擴展SherlockActivity {0}私人上下文context = this;

​​

我該如何解決這個問題?謝謝!

回答

2

您正在啓動一個活動以進行通知,首先您會看到一個空的活動 - 這正是您剛發佈的活動。改用BroadcastReceiver。

因此,當它接收:

public class Receiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 

    mBuilder.setContentTitle("Your fuel may be running low.") 
     .setContentText("There are 30 mins left on your timer. Check your fuel gauges.") 
     .setSmallIcon(R.drawable.ic_launcher); 

    Intent intent = new Intent(context, FuelTimer.class); 

    PendingIntent in = PendingIntent.getActivity(context, 0, intent, 0); 
    mBuilder.setContentIntent(in); 

    mNotificationManager.notify(0, mBuilder.build()); 

    } 
} 

您必須將接收器添加到清單:

<receiver android:name="com.YouForgotWhat.FlightGear.Receiver" > 
      <intent-filter> 
       <action android:name="com.YouForgotWhat.FlightGear.DisplayNotification" /> 
      </intent-filter> 
</receiver> 

最後,改變你的代碼,以啓動接收器,所以它就像

//PendingIntent to launch activity when the alarm triggers.      
Intent intent = new Intent("com.YouForgotWhat.FlightGear.DisplayNotification"); 

PendingIntent displayIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0); 
+0

工程就像一個魅力,謝謝! – NewGradDev