2013-03-01 31 views
0

我試圖正確地創建通知,但是因爲當您單擊通知時我無法執行操作。我希望你能幫助我或提供一些線索。在Android中執行操作單擊通知

+0

請張貼一些代碼,並清楚說明你的錯誤! – 2013-03-01 10:51:33

回答

0

建議使用Notification.BuilderNotificationCompat.Builder從支持庫建設的通知。

此外,爲什麼要重複使用被BroadcastReceiver抓到的Intent?它應該參考您想要通過點擊啓動的實際活動。

例如:

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

Notification notification = new NotificationCompat.Builder(context) 
       .setContentTitle(contentTitle) 
       .setContentText(contentText) 
       .setContentIntent(PendingIntent.getActivity(context, 0, intent, 0)) 
       .setAutoCancel(true) 
       .build(); 

如果你不真的要推出一些活動,你可能會開始Service或發送廣播消息來代替。見PendingIntent

+0

我想推出一個網站,我怎麼能做到這一點? – Squall 2013-03-01 16:27:52

+0

'Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(「http://www.google.com」));' 然後傳遞給 'PendingIntent.getActivity(context,0,intent,0) ' – 2013-03-02 08:15:56

+0

感謝您的幫助! – Squall 2013-03-02 18:47:16

0
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    Notification notif = new Notification(R.drawable.ic_launcher, "Text", 
      System.currentTimeMillis()); 

    Intent intent = new Intent(this, MainActivity.class); 
    intent.putExtra("somekey", "someextra"); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 

    notif.setLatestEventInfo(this, "Title", "Message", pIntent); 

    notif.flags |= Notification.FLAG_AUTO_CANCEL; 
    nm.notify(1, notif); 

使用Notification.Builder新版本的API

相關問題