Passing custom parcelable object extra or in ArrayList to RemoteViewsService breaks appwidget
Asked Answered
A

5

10

Basically what i want to do, is to pass a custom parcelable object via an intent, to the RemoteViewsService and thus to my RemoteViewsFactory. For some reason, this simply doesnt work :S Whenever i attempt to do this, the widget ends up displaying the "Problem loading widget" text instead.

When i try the same thing, but instead of passing it to the RemoteViewsService, i just pass it to a regular Activity, it works exactly as expected.

I've tried to find my answer online, but I've been unsuccessfull, hence i now post here in hope of being helped.

I've been trying to get this to work in my main program for a while. But i just couldnt figure it out. So i tried to boil it down, to see if somehow i did something wrong at some point. I succeded in recreating the problem, and the base on which i recreated my problem is from CommonsGuy's LoremWidget ( https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget ) and a random parcelable example ( http://prasanta-paul.blogspot.dk/2010/06/android-parcelable-example.htm l) which i've modified to work to show my problem. (hope i wont get into trouble, having used the code on these two links)

Heres the full code uploaded on random filehost in case anyone wants to testrun what i'm talking about: http://www.filedropper.com/remoteviewserviceparcelerrorexample

So to sum up my question:

Why can't i pass my parcelable objects (either alone, or wrapped in an ArrayList) to my RemoteViewsService? (When the same code works just fine with an Activity)

I really hope someone will be able to help me out. Right now in my main program im serializing to internal storage and then deserializing it again from the RemoteViewsService pretty much immedietly after, which from all I've read, can't be very effecient!

Edit: Just remembered, that for some reason in my main program, if i pass one of the custom objects, with the ArrayList and Strings set to null, and the booleans all to false (basically exactly how the object looks upon creation), the widget looks normal (ie. no "Problem loading widget" message).

Edit: After having looked at the look (not filtered to the app i was running) i now see some errors (guess i better learn to look at it all unfiltered when i get nothing filered :)):

     11-13 17:14:27.268: D/AndroidRuntime(8840): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
    11-13 17:14:27.268: D/AndroidRuntime(8840): CheckJNI is ON
    11-13 17:14:27.340: D/AndroidRuntime(8840): Calling main entry com.android.commands.pm.Pm
    11-13 17:14:27.348: W/ActivityManager(90): No content provider found for permission revoke: file:///data/local/tmp/com.commonsware.android.appwidget.lorem.LoremActivity.apk
    11-13 17:14:27.348: W/ActivityManager(90): No content provider found for permission revoke: file:///data/local/tmp/com.commonsware.android.appwidget.lorem.LoremActivity.apk
    11-13 17:14:27.356: I/PackageManager(90): Removing non-system package:com.commonsware.android.appwidget.lorem
    11-13 17:14:27.360: I/ActivityManager(90): Force stopping package com.commonsware.android.appwidget.lorem uid=10034
    11-13 17:14:27.360: I/ActivityManager(90): Killing proc 8798:com.commonsware.android.appwidget.lorem/10034: force stop
    11-13 17:14:27.400: D/dalvikvm(90): GC_CONCURRENT freed 511K, 17% free 8611K/10311K, paused 0ms+0ms
    11-13 17:14:27.400: I/PackageManager(90): Package com.commonsware.android.appwidget.lorem codePath changed from /data/app/com.commonsware.android.appwidget.lorem-2.apk to /data/app/com.commonsware.android.appwidget.lorem-1.apk; Retaining data and using new
    11-13 17:14:27.404: I/PackageManager(90): Running dexopt on: com.commonsware.android.appwidget.lorem
    11-13 17:14:27.440: D/dalvikvm(8850): DexOpt: load 12ms, verify+opt 4ms
    11-13 17:14:27.448: W/PackageManager(90): Code path for pkg : com.commonsware.android.appwidget.lorem changing from /data/app/com.commonsware.android.appwidget.lorem-2.apk to /data/app/com.commonsware.android.appwidget.lorem-1.apk
    11-13 17:14:27.448: W/PackageManager(90): Resource path for pkg : com.commonsware.android.appwidget.lorem changing from /data/app/com.commonsware.android.appwidget.lorem-2.apk to /data/app/com.commonsware.android.appwidget.lorem-1.apk
    11-13 17:14:27.452: I/ActivityManager(90): Force stopping package com.commonsware.android.appwidget.lorem uid=10034
    11-13 17:14:27.460: D/PackageManager(90): New package installed in /data/app/com.commonsware.android.appwidget.lorem-1.apk
    11-13 17:14:27.472: I/ActivityManager(90): Force stopping package com.commonsware.android.appwidget.lorem uid=10034
    11-13 17:14:27.496: D/dalvikvm(266): GC_EXPLICIT freed 127K, 9% free 6766K/7367K, paused 0ms+0ms
    11-13 17:14:27.512: D/dalvikvm(221): GC_EXPLICIT freed 878K, 57% free 15010K/34119K, paused 0ms+0ms
    11-13 17:14:27.584: D/dalvikvm(90): GC_EXPLICIT freed 385K, 18% free 8558K/10311K, paused 0ms+0ms
    11-13 17:14:27.588: D/PackageManager(90): generateServicesMap(android.accounts.AccountAuthenticator): 2 services unchanged
    11-13 17:14:27.620: D/PackageManager(90): generateServicesMap(android.content.SyncAdapter): 4 services unchanged
    11-13 17:14:27.620: D/BackupManagerService(90): Received broadcast Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:com.commonsware.android.appwidget.lorem flg=0x10000010 (has extras) }
    11-13 17:14:27.620: D/PackageManager(90): generateServicesMap(android.accounts.AccountAuthenticator): 2 services unchanged
    11-13 17:14:27.620: D/PackageManager(90): generateServicesMap(android.content.SyncAdapter): 4 services unchanged
    11-13 17:14:27.628: W/ResourceType(90): Failure getting entry for 0x7f060000 (t=5 e=0) in package 0 (error -75)
    11-13 17:14:27.632: D/BackupManagerService(90): Received broadcast Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.commonsware.android.appwidget.lorem flg=0x10000010 (has extras) }
    11-13 17:14:27.636: V/BackupManagerService(90): updatePackageParticipantsLocked: #1
    11-13 17:14:27.640: W/RecognitionManagerService(90): no available voice recognition services found
    11-13 17:14:27.652: D/dalvikvm(8854): Not late-enabling CheckJNI (already on)
    11-13 17:14:27.684: I/ActivityManager(90): Start proc com.commonsware.android.appwidget.lorem for broadcast com.commonsware.android.appwidget.lorem/.WidgetProvider: pid=8854 uid=10034 gids={}
    11-13 17:14:27.688: D/BackupManagerService(90): Received broadcast Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:com.commonsware.android.appwidget.lorem flg=0x10000010 (has extras) }
    11-13 17:14:27.688: V/BackupManagerService(90): updatePackageParticipantsLocked: #1
    11-13 17:14:27.740: I/dalvikvm(8854): Turning on JNI app bug workarounds for target SDK version 11...
    11-13 17:14:27.756: D/dalvikvm(90): GC_EXPLICIT freed 409K, 16% free 8687K/10311K, paused 0ms+4ms
    11-13 17:14:27.792: D/AndroidRuntime(8840): Shutting down VM
    11-13 17:14:27.796: D/dalvikvm(8840): GC_CONCURRENT freed 99K, 79% free 447K/2048K, paused 0ms+0ms
    11-13 17:14:27.796: D/dalvikvm(8840): Debugger has detached; object registry had 1 entries
    11-13 17:14:27.812: I/AndroidRuntime(8840): NOTE: attach of thread 'Binder Thread #3' failed
    11-13 17:14:27.820: D/WidgetProvider(8854): ParcelData=ParcelData [id=0, name=null, desc=null, cities=[suwon, delhi]]
    11-13 17:14:27.820: D/WidgetProvider(8854): ArrayList<ParcelData>=[ParcelData [id=0, name=null, desc=null, cities=[suwon, delhi]]]
    11-13 17:14:27.824: V/ParcelData(8854): writeToParcel...0
    11-13 17:14:27.824: V/ParcelData(8854): writeToParcel...0
    11-13 17:14:27.828: V/ParcelData(8854): writeToParcel...0
    11-13 17:14:27.828: V/ParcelData(8854): writeToParcel...0
    11-13 17:14:27.828: E/Parcel(221): Class not found when unmarshalling: com.commonsware.android.appwidget.lorem.ParcelData, e: java.lang.ClassNotFoundException: com.commonsware.android.appwidget.lorem.ParcelData
    11-13 17:14:27.832: W/AppWidgetHostView(221): updateAppWidget couldn't find any view, using error view
    11-13 17:14:27.832: W/AppWidgetHostView(221): android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.commonsware.android.appwidget.lorem.ParcelData
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.os.Parcel.readParcelable(Parcel.java:1966)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.os.Parcel.readValue(Parcel.java:1854)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.os.Parcel.readListInternal(Parcel.java:2103)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.os.Parcel.readArrayList(Parcel.java:1544)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.os.Parcel.readValue(Parcel.java:1875)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.os.Parcel.readMapInternal(Parcel.java:2094)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.os.Bundle.unparcel(Bundle.java:223)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.os.Bundle.putInt(Bundle.java:436)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.content.Intent.putExtra(Intent.java:4695)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.widget.RemoteViews$SetRemoteViewsAdapterIntent.apply(RemoteViews.java:401)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.widget.RemoteViews.performApply(RemoteViews.java:1606)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.widget.RemoteViews.apply(RemoteViews.java:1583)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:289)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:283)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.appwidget.AppWidgetHost$UpdateHandler.handleMessage(AppWidgetHost.java:84)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.os.Handler.dispatchMessage(Handler.java:99)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.os.Looper.loop(Looper.java:137)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at android.app.ActivityThread.main(ActivityThread.java:4424)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at java.lang.reflect.Method.invokeNative(Native Method)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at java.lang.reflect.Method.invoke(Method.java:511)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    11-13 17:14:27.832: W/AppWidgetHostView(221):   at dalvik.system.NativeStart.main(Native Method)
    11-13 17:14:28.116: D/AndroidRuntime(8872): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
    11-13 17:14:28.116: D/AndroidRuntime(8872): CheckJNI is ON
    11-13 17:14:28.200: D/AndroidRuntime(8872): Calling main entry com.android.commands.am.Am
    11-13 17:14:28.204: I/ActivityManager(90): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.commonsware.android.appwidget.lorem/.LoremActivity} from pid 8872
    11-13 17:14:28.208: W/WindowManager(90): Failure taking screenshot for (192x135) to layer 21020
    11-13 17:14:28.220: W/NetworkManagementSocketTagger(90): setKernelCountSet(10034, 1) failed with errno -2
    11-13 17:14:28.240: D/AndroidRuntime(8872): Shutting down VM
    11-13 17:14:28.244: D/dalvikvm(8872): GC_CONCURRENT freed 100K, 77% free 475K/2048K, paused 4ms+0ms
    11-13 17:14:28.244: D/dalvikvm(8872): Debugger has detached; object registry had 1 entries
    11-13 17:14:28.244: I/AndroidRuntime(8872): NOTE: attach of thread 'Binder Thread #1' failed
    11-13 17:14:28.252: W/InputManagerService(90): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@b38c3ef0
    11-13 17:14:28.504: W/NetworkManagementSocketTagger(90): setKernelCountSet(10034, 0) failed with errno -2
Ap answered 13/11, 2012 at 14:59 Comment(6)
Please post any relevant errors from the logcat. You should see some problems logged there.Allowed
Well the thing is, that i don't see any errors in the logcat, unless i try to print information from the parcel im passing (in which case i just get a null pointer). Would you still like to see this logcat error? The only thing that tells me something is wrong, is the widget itself that shows "Problem loading widget" and the fact that Log messages posted from RemoteViewsService and factory, are never printed (ie. they are never called or reached for some reason).Ap
You are sure you don't see anything at all in the logcat? Are you filtering the logcat at all?Allowed
Updated the question with an unfiltered log, that actually gives clues :) ThanksAp
The problem is, as I suspected, that you are getting a ClassNotFoundException when trying to unparcel your custom object. You can try to call svcIntent.setExtrasClassLoader(ParcelData.class.getClassLoader()) on the Intent when you put the extras into it. I'm not sure if this will work though.Allowed
I'm afraid that didnt do the trick. But atleast this log gives me more clues to search on! If you got any more ideas i'd be glad to hear them :)Ap
A
3

I ended up doing what TreKing talks about here: https://groups.google.com/d/msg/android-developers/KX0BUAbOTKY/jqW_ZokCH3gJ

What I've done is create a "Bundleable" interface that basically does what Parcelable is intended to do. Objects extending this interface can put themselves and recreate themselves from a Bundle object, which is itself Parcelable so you can send it around just like your object - except with the minor fact that the system always knows how to load a Bundle type so you don't run into this error.

And following his code example:

public interface Bundleable
{
 public Bundle toBundle();

 public void fromBundle(Bundle b);
}

public class MyClass implements Bundleable
{
 public Bundle toBundle()
 { 
  Bundle b = new Bundle();
  // Fill b with data
  return b;
 }

 public void from Bundle(Bundle b)
 {
  // set properties from data in b
 }
}

// ...

MyClass m = new MyClass();
Intent i = new Intent();
i.putBundleExtra("MyClass", m.toBundle());

// ... Elsewhere

Bundle b = intent.getBundleExtra("MyClass");
MyClass m = new MyClass(b); // Constructor calls fromBundle(b);

Worked like i'd expected the whole Parcel deal to do from the beginning! :)

Ap answered 14/11, 2012 at 18:55 Comment(0)
S
3

I had the same problem with a Serializable object ArrayList. When I tried to pass the list in the AppWidgetManager like this

Intent listIntent = new Intent(context, WidgetService.class);
listIntent.putExtra("list", myList);

it just did not work. But putting the same data in a bundle and then putting that in the intent as an extra worked.

Intent listIntent = new Intent(context, WidgetService.class);
Bundle extrasBundle = new Bundle();
extrasBundle.putSerializable("list", myList);
listIntent.putExtra("bundle", extrasBundle);

I found a post here on stack overflow where users explain the difference between putting the extra directly or in a bundle: Advantages of using Bundle instead of direct Intent putExtra() in Android And it looks like it works because only if you put an extra into a bundle, it is possible to pass it through multiple intents.

Shuttle answered 24/3, 2018 at 12:21 Comment(0)
A
0

If you cannot get this to work using Parcelable, just do it yourself. Pass the ParcelData as a String instead. Just convert your writeToParcel() and ParcelData(Parcel source) methods to something like this (poor-man's "serialization"):

public String serializeToString() {
    StringBuilder sb = new StringBuilder();
    sb.append(Integer.toString(id)).append('|').append(name).append('|').append(desc)
        .append('|');
    for (String c : cities) {
        sb.append(c).append(';');
    }
    sb.append('|');
    return sb.toString();
}

ParcelData(String source) {
    String[] parts = source.split("|");
    id = Integer.parseInt(parts[0]);
    name = parts[1];
    desc = parts[2];
    cc = parts[3].split(";");
    if (cc.length == 0) {
        cities = new String[0]; // No cities
    } else {
        cities = new String[cc.length - 1]; // Last element is empty
        for (int i = 0; i < cc.length - 1; i++) {
            cities[i] = cc[i];
        }
    }
}

I didn't run this through a compiler, so it may have stupid syntax mistakes. You should get the idea.

Allowed answered 13/11, 2012 at 18:16 Comment(3)
I ended up doing what I've written as my answer. I thank you for forcing me to look at the unfiltered logs. The clues to my success where indeed right there :)Ap
Glad you were able to resolve it. Filtering logs is usually a bad idea ;-)Allowed
Heh, well usually the un-filtered log is spammed with so much different info, that its hard to actually keep track of what matters to me. Which is why i usually always filter it to the app im working on. I'll definetly be sure to check-out the unfiltered one if i run in any other problems that arent shown in the filtered one! :)Ap
U
0

I know it's too late, and I mean tooo late, but today I've ran into the same problem, and found a solution with a minimum lines of code. Decided to share with the solution. So, basically you can convert any object, even a list of objects, to a string using the Gson API from Google. Check it out:

ComplextObject object=new ComplexObject();
Type type = new TypeToken<ComplextObject>() {}.getType();
intent.putExtra("key",convertToJsonString(object,type)); //that's it

Or read this object "on the other side":

String jsonString=intent.getStringExtra("key");
Type type = new TypeToken<ComplexObject>() {}.getType();
ComplexObject object=convertFromJsonString(jsonString,type);

And here are the convertToJsonString and convertFromJsonString methods:

public static <T> T convertFromJsonString(String jsonString, Type type){
    if(jsonString==null) return null;
    Gson gson=new Gson();
    return gson.fromJson(jsonString,type);
}

public static String convertToJsonString(Object object, Type type){
    if(object==null) return null;
    Gson gson=new Gson();
    return gson.toJson(object,type);
}
Unstep answered 3/6, 2017 at 0:23 Comment(0)
S
0

Here is a sample code using Kotlin and Bundle:

In you updateAppWidget()

val intent = Intent(context, ItemsCollectionRemoteViewsService::class.java)
            .apply {
                val extrasBundle = Bundle()
                extrasBundle.putSerializable(WIDGET_DATA, myCustomData)
                putExtra(WIDGET_BUNDLE, extrasBundle)
            }
        remoteViews.setRemoteAdapter(R.id.items_list_view, intent)

Then in RemoteViewsService.RemoteViewsFactory

override fun onCreate() {
    val bundleData = data?.getBundleExtra(WIDGET_BUNDLE)
    bundleData?.let {
        val myCustomData = bundleData.getSerializable(WIDGET_DATA) as MyCustomData?
        myCustomData?.let {
            list = myCustomData.myList
        }
    }
}
Streaky answered 25/1, 2022 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.