3

我需要将一个字符串数组从 AlarmReceiver.class 传递给 Notify.class,但该字符串始终为“null”。现在,AlarmReceiver 中的意图有问题吗?

public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
[...]
Intent notificationIntent = new Intent("com.example.myapp", null, context, Notify.class);
        notificationIntent.putExtra("notify", not1[x]);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

通知班级:

@Override
    public void onCreate(Bundle savedInstanceState){ 
      super.onCreate(savedInstanceState);
Intent notificationIntent = getIntent();
      String[] message = notificationIntent.getStringArrayExtra("notify");
Toast toast6=Toast.makeText(this,""+message,Toast.LENGTH_LONG);
        toast6.show();
4

1 回答 1

7

您正在使用两种不同的方法...

  1. 在这里,您正在使用一个字符串:

    notificationIntent.putExtra("notify", not1[x]); // Pass one String
    

    您应该阅读以下内容"notify"

    String message = notificationIntent.getStringExtra("notify");
    
  2. 如果要传递字符串数组,请使用:

    notificationIntent.putExtra("notify", not1); // Pass array of Strings
    

    您应该阅读以下内容:

    String[] message = notificationIntent.getStringArrayExtra("notify");
    

你看得到差别吗?

于 2012-12-21T19:50:45.537 回答