Pendingintent getbroadcast lost parcelable data

前端 未结 2 1659
南方客
南方客 2020-12-17 20:42

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 reveiv

2条回答
  •  旧巷少年郎
    2020-12-17 21:34

    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");
            }
    
        }
    

提交回复
热议问题