在通知期间无法通过意图传输数据

时间:2014-02-16 07:01:33

标签: android android-intent notifications android-notifications

我一直在stackoverflow中搜索很多问题和答案。我仍然无法解决我的问题。

我设置广播特定时间。 我在另一个类“AlarmReceiver.java”中完美地接收了广播,该类在接收广播时创建通知。 但是我通过一个字符串传递一个字符串,在按下通知时调用类“AlarmResponder.java”。

课程正在被召唤。 我只收到第一次通过意图的字符串数据。 下次我什么也得不到。 getextras()给我null。

如果我在NotificationManager.notify(1,mBuilder.build())中更改通知的ID;如 NotificationManager.notify(2,mBuilder.build());它有效。

但它总是只能工作一次 请帮帮我

 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);
        }
    }
}

}

0 个答案:

没有答案