意图和字符串数组

Pol*_*len 2 android

我需要将一个字符串数组从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);
Run Code Online (Sandbox Code Playgroud)

通知课程:

@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();
Run Code Online (Sandbox Code Playgroud)

Sam*_*Sam 6

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

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

    notificationIntent.putExtra("notify", not1[x]); // Pass one String
    
    Run Code Online (Sandbox Code Playgroud)

    你应该阅读"notify":

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

    notificationIntent.putExtra("notify", not1); // Pass array of Strings
    
    Run Code Online (Sandbox Code Playgroud)

    你应该阅读:

    String[] message = notificationIntent.getStringArrayExtra("notify");
    
    Run Code Online (Sandbox Code Playgroud)

你看得到差别吗?