我试图使用pendingIntent设置通知。但我无法收到我发送的数据。
这些是代码
显示通知的类(正常工作我可以从意图中获取每个数据)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---get the notification ID for the notification;
// passed in by the MainActivity---
int notifID = getIntent().getExtras().getInt("id");
String date = getIntent().getExtras().getString("date");
String time = getIntent().getExtras().getString("time");
String text = getIntent().getExtras().getString("text");
//---PendingIntent to launch activity if the user selects
// the notification---
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(
R.drawable.ic_launcher,
text +" "+date+" @ "+time,
System.currentTimeMillis());
notif.flags = Notification.FLAG_AUTO_CANCEL;
CharSequence from = "TODO Note";
CharSequence message = text;
Intent p = new Intent(this,NotifThrow.class);
p.putExtra("id",notifID);
p.putExtra("date",date);
PendingIntent detailsIntent =
PendingIntent.getActivity(this, 0, p, 0);
notif.setLatestEventInfo(this, from, message, detailsIntent);
//---100ms delay, vibrate for 250ms, pause for 100 ms and
// then vibrate for 500ms---
notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(notifID, notif);
//---destroy the activity---
finish();
}
}
这个类(下面)应该在我触摸通知时起作用(它可以工作但没有来自意图的数据)
public class NotifThrow extends Activity
{
TodoDataSource dataSource;
SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int notifID = getIntent().getExtras().getInt("id");
String dates = getIntent().getExtras().getString("date");
Log.i("Message From Notification Touch","Id"+notifID);
dataSource = new TodoDataSource(this);
dataSource.alarmToggle(notifID);
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
id,日期显示为空
我在这里做错了什么? 提前谢谢..
答案 0 :(得分:0)
有一个解决方法
在类NotifThrow.java中设置一个public static int id并从外部更新它
答案 1 :(得分:0)
如果您正在调用的活动实例已在运行,那么它将调用onNewIntent()而不是创建新实例。来自getIntent()
的{{1}}始终返回用于启动onCreate
(第一个)的Intent。