2012-12-14 86 views
0

我想做一個簡單的功能,安排在Android本地通知,但它不工作。我錯過了什麼嗎?一世。即清單中的許可或類似的東西?我是android編程的新手。我的Android通知日程安排功能有什麼問題?

static int notificationId = 0; 

public void scheduleLocalNotification(String text, int seconds) 
{ 
    Context context = getApplicationContext(); 
    long when = System.currentTimeMillis() + seconds * 1000; 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent notificationIntent = new Intent(context, MyApp.class); 

    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = PendingIntent.getActivity(context, 0, 
      notificationIntent, 0); 
    Notification notification = new Notification(); 
    notification.setLatestEventInfo(context, "My application", text, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.when = when; 
    notificationManager.notify(notificationId, notification); 
    notificationId ++; 

} 
+0

定義「不起作用」。它是無所作爲還是會出現某種錯誤?如果您收到錯誤消息,請發佈logcat,以便我們可以更輕鬆地幫助您 – codeMagic

+0

'PendingIntent.getActivity(context,0,notificationIntent,0);'您對'flags'參數使用0,該參數不是有效值。使用PendingIntent FLAG_常量之一。 – Squonk

+0

@codeMagic是一個無提示錯誤。一切似乎都很好,但通知不會觸發。只是。 – Chema

回答

1

我想你是忘了Android清單文件中的新活動的條目下

<activity 
     android:name=".your activity name" 
     /> 

和運行程序

+0

我有條目'' – Chema

0

我發現它所以只是條目的活動等給出。問題是我沒有將圖標資源傳遞給通知構造函數。

notification = new Notification(R.drawable.icon, text, when);