Android 通知期间无法通过意图传输数据
我在stackoverflow中搜索了很多问题和答案。我仍然不能解决我的问题 我为特定时间设置广播。 我在另一个类“AlarmReceiver.java”中完美地接收广播,该类在接收广播时创建通知。 但是我通过一个意图传递一个字符串,该意图在按下通知时调用一个类'AlarmResponder.java' 全班都被叫来了。 我仅第一次接收通过intent传递的字符串数据。 下次我什么也得不到。getextras()为空 如果我在NotificationManager.notify(1,mBuilder.build())中更改通知的ID;作为 NotificationManager.notify(2,mBuilder.build());它起作用了 但它总是只工作一次 请帮帮我Android 通知期间无法通过意图传输数据,android,android-intent,notifications,android-notifications,Android,Android Intent,Notifications,Android Notifications,我在stackoverflow中搜索了很多问题和答案。我仍然不能解决我的问题 我为特定时间设置广播。 我在另一个类“AlarmReceiver.java”中完美地接收广播,该类在接收广播时创建通知。 但是我通过一个意图传递一个字符串,该意图在按下通知时调用一个类'AlarmResponder.java' 全班都被叫来了。 我仅第一次接收通过intent传递的字符串数据。 下次我什么也得不到。getextras()为空 如果我在NotificationManager.notify(1,mBuild
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
CharSequence reminderText = intent.getCharSequenceExtra("reminderText");
if (reminderText != null && reminderText != "") {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
//Toast.makeText(context, intent.getCharSequenceExtra("reminderText"), Toast.LENGTH_LONG).show();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(reminderText)
.setContentText("its time buddy");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, AlarmResponder.class);
resultIntent.putExtra("reminderText", reminderText);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(AlarmResponder.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(1, mBuilder.build());
}
}
}
AlarmResponder类是
public class AlarmResponder extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
if(extras != null){
if(extras.containsKey("reminderText"))
{
setContentView(R.layout.alarm_responder);
TextView reminderText = (TextView) findViewById(R.id.reminderText);
// extract the extra-data in the Notification
String msg = extras.getString("reminderText");
reminderText.setText(msg);
}
}
}
}