2011-08-25 46 views
12

我使用下面的代码时启动一个服务已启动通过AlarmManager通知:如何设置通知的点击监听器?

nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
CharSequence from = "App"; 
CharSequence message = "Getting Latest Info..."; 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0); 
Notification notif = new Notification(R.drawable.icon, 
    "Getting Latest Info...", System.currentTimeMillis()); 
notif.setLatestEventInfo(this, from, message, contentIntent); 
nm.notify(1, notif); 

如何设置意图为这个项目,这样当用户点击它,它会推出一定的活动?

回答

12

您基本上需要将Activity类作为您的意图的一部分放入PendingIntent。目前你的意图是空的。重定向到新的活动,它应该是:

// This line of yours should contain the activity that you want to launch. 
// You are currently just passing empty new Intent() 
PendingIntent contentIntent = 
    PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0); 

+0

你能举个例子吗? – yoshi24

+0

我已经添加了相关的代码,您需要修改 – momo

+0

另外我知道这个问题以外,有无论如何,我可以设置一个额外的通过它? – yoshi24

23

至于yoshi24的评论,你可以设置额外这样。

final Intent intent = new Intent(this, MyActivity.class); 
intent.setData(data); 
intent.putExtra("key", "value"); 
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 

您需要以及准备挂起意图

https://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity

UPDATE 一些像这样的事情会为你

INT您mainfest

工作之前,要意识到这一点
<activity android:name=".MyActivity" android:launchMode="singleTop" ... /> 

您的活动

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    processIntent(getIntent()); 
} 

@Override 
protected void onNewIntent(Intent intent) {  
    processIntent(intent); 
}; 

private void processIntent(Intent intent){ 
    //get your extras 
} 
+0

你发布的链接是指什么,我正在尝试做什么? – yoshi24

+1

它与您发布的 – Samuel

+1

相同,您必须在清单中将意图标记更改为** android:launchMode =“singleTask”**,并在活动内重写** onNewIntent(意图intent)**。这将使您能够接收参数。 – Samuel

6

我做到了,

  • 我添加Intent.FLAG_ACTIVITY_CLEAR_TOP新意图

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.ic_launcher, 
         "A new notification", System.currentTimeMillis()); 
    // Hide the notification after its selected 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    
    Intent intent = new Intent(this, NoficationDemoActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    Bundle bundle = new Bundle(); 
    bundle.putString("buzz", "buzz"); 
    intent.putExtras(bundle); 
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); 
    notification.setLatestEventInfo(this, "This is the title", 
         "This is the text", activity); 
    notification.number += 1; 
    notificationManager.notify(0, notification); 
    
  • 的onCreate我如下操作:

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    if(getIntent().getExtras()!=null){ 
        Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show(); 
    }