13

Here is the problem. My program is running perfect in Android 6.0. After update the device to android 7.0. Pendingintent can not pass the parcelable data to boradcast reveiver. Here is the code.

Fire the alarm

public static void setAlarm(@NonNull Context context, @NonNull Todo todo) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra("KEY_TODO", todo);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, todo.remindDate.getTime(), alarmIntent);
}

Todo is a Parcelable class while todo is the instance I need in notification.

In Broadcastreceiver, I cannot getParcelable data.

public void onReceive(Context context, Intent intent) {

    Todo todo = intent.getParcelableExtra("KEY_TODO");

}

Here is the result of intent when I debug

enter image description here

I dont know why the intent only contains a Integer that I never put it in. Where is the Parcelable todo. This code has no problem in android 6.0, but can not run in 7.0

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
BIN
  • 145
  • 1
  • 10
  • Have you tried wrapping your `Todo` object in a `Bundle` before adding it to the "extras"? This usually works when passing custom `Parcelable` objects to the `AlarmManager` (but may now be broken in Android 7). I would be interested in your findings. – David Wasser Sep 15 '16 at 11:05
  • To add extra: `Bundle bundle = new Bundle; bundle.putParcelable("todo", todo); intent.putExtra("KEY_TODO", bundle);`. To extract extra: `Bundle bundle = intent.getBundleExtra("KEY_TODO"); if (bundle != null) { Todo todo = bundle.getParcelableExtra("todo"); }` – David Wasser Sep 15 '16 at 11:10

2 Answers2

22

Quoting myself:

Custom Parcelable classes — ones unique to your app, not a part of the Android framework — have had intermittent problems over the years when used as Intent extras. Basically, if a core OS process needs to modify the Intent extras, that process winds up trying to recreate your Parcelable objects as part of setting up the extras Bundle for modification. That process does not have your class and so it gets a runtime exception.

One area where this can occur is with AlarmManager. Code that used custom Parcelable objects with AlarmManager that might have worked on older versions of Android will not work on Android N.

The most efficient workaround that I know of is to manually convert the Parceable yourself into a byte[] and put that in the Intent extra, manually converting it back into a Parcelable as needed. This Stack Overflow answer shows the technique, and this sample project provides a complete working sample.

The key bits are the conversions between the Parcelable and the byte[]:

/***
 Copyright (c) 2016 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 From _The Busy Coder's Guide to Android Development_
 https://commonsware.com/Android
 */

package com.commonsware.android.parcelable.marshall;

import android.os.Parcel;
import android.os.Parcelable;

// inspired by https://stackoverflow.com/a/18000094/115145

public class Parcelables {
  public static byte[] toByteArray(Parcelable parcelable) {
    Parcel parcel=Parcel.obtain();

    parcelable.writeToParcel(parcel, 0);

    byte[] result=parcel.marshall();

    parcel.recycle();

    return(result);
  }

  public static <T> T toParcelable(byte[] bytes,
                                   Parcelable.Creator<T> creator) {
    Parcel parcel=Parcel.obtain();

    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0);

    T result=creator.createFromParcel(parcel);

    parcel.recycle();

    return(result);
  }
}
Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    you save my day !!!!!!!!!!!!!!!!!!!!!!!!!!!!! It is the Android N system that i can not pass the parcelable data with AlarmManager. – BIN Sep 13 '16 at 22:04
  • Thanks CommonsWare for telling us this limitation. Your answer is a big help. By knowing this limitation, we have intention to only pass around DB Id. When the alarm being triggered, is it safe to perform SQLite query (Using Room) based on the received DB Id? – Cheok Yan Cheng Jun 02 '18 at 19:35
  • 1
    @CheokYanCheng: I am not certain what you mean by "safe". You are subject to all the standard limitations: do not do I/O on the main application thread, background services can run for only one minute on Android 8.0+, etc. – CommonsWare Jun 02 '18 at 19:43
  • I get your point, and I understand limitation of starting service since Android O. May I know, if I let `Executors.newSingleThreadExecutor` to execute DB operation (write few row into table) within BroadcastReceiver's onReceive, is it OK to do so? I had tested, it works so far. However, I'm not sure whether there's any edge cases. – Cheok Yan Cheng Jun 02 '18 at 21:04
  • @CheokYanCheng: "is it OK to do so?" -- in the absence of a service or something else keeping your process around, once `onReceive()` returns, your process can be terminated at any point. This might be before your thread is done with its work. `goAsync()` would help, or delegate the work to a service. – CommonsWare Jun 02 '18 at 21:21
  • Launching another service is too much for simple DB update. But, goAsync seems like the way. Thanks for telling me. I hope it wouldn't return null. – Cheok Yan Cheng Jun 02 '18 at 22:02
15

Following the suggesting of @David Wasser in the comments underneath the question, I was able to resolve the same problem like this:

    public static void setAlarm(@NonNull Context context, @NonNull Todo todo) {

      ...

      Intent intent = new Intent(context, AlarmReceiver.class);

      Bundle todoBundle = new Bundle();
      todoBundle.putParcelable("KEY_TODO", todo);

      intent.putExtra("KEY_TODO", todoBundle); // i just reuse the same key for convenience

      ...

    }

Then in the broadcast receiver extract the bundle like this:

    public void onReceive(Context context, Intent intent) {

        Bundle todoBundle = intent.getBundleExtra("KEY_TODO");

        Todo todo;

        if (todoBundle != null ) {
            todo = todoBundle.getParcelable("KEY_TODO");
        }

    }
Njuacha Hubert
  • 388
  • 3
  • 14