Android Cannot Pass Intent Extras Though Alarmmanager

Android cannot pass intent extras though AlarmManager

I have some precisions that could help others, to be associated with the solution from Someone Somewhere. If you pass custom Parcelable objects as an extra, the operating system may not be able to process them, so an internal exception occurs and your extras are lost.

With Android N, even with PendingIntent.FLAG_UPDATE_CURRENT I cannot retrieve my custom Pacelable extras.

So I had to use system known Parcelable (like ParcelUuid) to reference some objects in a custom database instead of providing my whole Parcelable object.

Another solution is to convert Parcelable to a byte array that is correctly recognized by the system: How to marshall and unmarshall a Parcelable to a byte array with help of Parcel?

Passing values using intent extras in Alarm Manager in android

I can see problems arising from this method. What I would do is store your two variables in a SharedPrefs file and access them from the BroadcastReciever directly. SharedPrefs values persist while "global variables" in Android do not.

Android AlarmManager Pass extra from activity to service

Turns out instead on using it on create, I have to use onStartCommand. Here's the code:

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Toast.makeText(this, "Starting..", Toast.LENGTH_SHORT).show();
Log.d("0",intent.getStringExtra("newtitle"));

title = intent.getStringExtra("newtitle");
content = intent.getStringExtra("newcontent");
Intent myintent = new Intent(this.getApplication(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,myintent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Action action = new NotificationCompat.Action.Builder(
R.drawable.basketball, getString(R.string.wearTitle), pendingIntent
).build();

Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Notification notification = new NotificationCompat.Builder(this)
.setContentText(content)
.setContentTitle(title)
.setSmallIcon(R.drawable.basketball)
.setSound(sound)
.extend(new NotificationCompat.WearableExtender().addAction(action))
.build();

NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(001, notification);
return PendingIntent.FLAG_UPDATE_CURRENT; // or whatever your flag
}

Got it from: Pass data from Activity to Service using an Intent

Getting extras from an intent through a bundle won't allow updated values

try changing this:

PendingIntent pi = PendingIntent.getBroadcast(context, 0 , repeatIntent, 0);

to this:

PendingIntent pi = PendingIntent.getBroadcast(context, 0 , repeatIntent, PendingIntent.FLAG_UPDATE_CURRENT);

The problem is probably that your intent is not refreshed when new alarm is fired.

Android 7 intent extras missing

Putting a custom Parcelable in a PendingIntent has never been especially reliable, and it flat-out will not work in an AlarmManager PendingIntent on Android 7.0. Other processes may need to fill in values into the Intent, and that involves manipulating the extras, and that can't be done in any process but your own, since no other process has your custom Parcelable class.

This SO answer has a workaround, in the form of converting the Parcelable yourself to/from a byte[].

intent.putExtra() in pending intent not working

Try this

Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);

PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
id,
aint,
// as stated in the comments, this flag is important!
PendingIntent.FLAG_UPDATE_CURRENT);


Related Topics



Leave a reply



Submit