我正在尝试从通知栏启动一个活动,但它需要获取一个int。从活动到活动我只想在意图中加入一个int,但我似乎无法弄清楚在通知栏中添加额外内容的位置?这可能吗?如果不是,有更好的方法吗?
public void notifyMe() {
int icon = android.R.drawable.presence_away;
CharSequence tickerText = "New Task Availible";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "PRISM Task";
CharSequence contentText = "New Task Availible";
Intent notificationIntent = new Intent(this, TextAnswerActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notificationIntent.putExtra("taskId", 20); //unable to retrieve 20 later
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
我如何检索:
Intent myIntent = getIntent();
System.out.println("#######TaskID from Intent ===== " + myIntent.getIntExtra("taskId", -1));
答案 0 :(得分:4)
在创建将notificationIntent作为参数的PendingIntent之前,您必须调用notificationIntent.putExtras(...)。试试这个代码:
Intent notificationIntent = new Intent(this, TextAnswerActivity.class);
notificationIntent.putExtra("taskId", 20);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
并接收数据:
Bundle b = getIntent().getExtras();
if (b != null) {
int taskId = b.getInt("taskId");
System.out.println("############# taskID: " + taskId);
}