3

I need pass a string array from AlarmReceiver.class to Notify.class but the string is always "null". Now, is a problem about intent within 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);

Notify class:

@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();
2
  • What are the declarations for not1 and x? Commented Dec 21, 2012 at 19:51
  • You are mixing string array with a plain string. which do you want? Commented Dec 21, 2012 at 19:54

1 Answer 1

7

You are using two different methods...

  1. Here you're are working with one String:

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

    You should read this "notify" with:

    String message = notificationIntent.getStringExtra("notify");
    
  2. If you want to pass the array of Strings use:

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

    You should read this with:

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

Do you see the difference?

Sign up to request clarification or add additional context in comments.

4 Comments

Or the other way around, putExtra("notify", not1); as it seems the question asks about sending an array. Not that it is used that way so a bit confusing.
Thanks Sam! You're always so gentle! :-)
Sam: what it the best way to cancel notify after click on it?
You can call NotificationManager#cancel(), using the id that you used in notify().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.