Shiva Krishna Bavandla
Shiva Krishna Bavandla

Reputation: 26668

How to add and retrieve data from an intent in android

I was trying to implement push notifications in android and below is my receiver code

public class MessageReceiver extends BroadcastReceiver {
    public void onReceive(Context ctx, Intent intent) {
        Intent myIntent = new Intent(ctx, MainActivity.class);
        myIntent.putExtra("skipList", true);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                ctx, 0, myIntent, 0);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(ctx)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(intent.getExtras().getString("subject"))
                        .setContentText(intent.getExtras().getString("cover"))
                        .setContentIntent(pendingIntent);
        mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
        NotificationManager mNotificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
}

So from the above code i was adding the data skipList = True to intent myIntent

I just want to retrieve this data in my MainActivity.java file as below

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            Intent iin= getIntent();
            Log.v("<<<<<<<<<<<<iiiiiiiii>>>>>>>>>>>>>>>>",iin.toString());
            Bundle b = iin.getExtras();
            Log.v("<<<<<<<<<<<<bbbbbbbb>>>>>>>>>>>>>>>>",b.toString());
            String wow = b.getString("skipList");
            Log.v("<<<<<<<<<<<<wowowowoow>>>>>>>>>>>>>>>>",wow.toString());
}

But i was unable to get the data from the intent what am i doing wrong ?

Upvotes: 0

Views: 78

Answers (5)

Rahul Sharma
Rahul Sharma

Reputation: 6169

You are using putExtra with a boolean parameter, but you are retrieving it as a String. You need to get it using the getBooleanExtra() method. Just replace this line:

String wow = b.getString("skipList");

with this:

boolean wow = b.getBooleanExtra("skipList");

Upvotes: 1

Saritha
Saritha

Reputation: 488

In code your passing boolean data through intent,but getting it as a string: try like this,

   boolean isSkipList = getIntent().getExtras().getBoolean("skipList");

(or)

replace below line:

  String wow = b.getString("skiplist");

with this line:

  boolean wow = b.getBoolean("skiplist");

Upvotes: 0

Deven Singh
Deven Singh

Reputation: 418

you should write

 boolean wow = b.getBooleanExtras("skipList");

or

 myIntent.putExtra("skipList", "true");

Upvotes: 0

user4571931
user4571931

Reputation:

You are passing boolean value so you should read boolean to use following code

getIntent().getBooleanExtra("skipList", defaultValue);

This may help you.

Upvotes: 0

NoSixties
NoSixties

Reputation: 2533

You are passing a boolean in your putExtra and try to retrieve it as a String.

I think this tutorial might be useful for you to gain some more knowledge about what you can do with intents

Upvotes: 0

Related Questions