Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

android - Location Client request location updates with parcelable extras in PendingIntent

I am using LocationClient with PendingIntent to get location updates.

PendingIntent.getService(context, 0, new Intent(context, OnLocationAvail.class), PendingIntent.FLAG_UPDATE_CURRENT)

The Above code works fine I get the location from the key LocationClient.KEY_LOCATION_CHANGED

But when I have an extras of parcelable data as described below, the service gets called with the parcelable data but the key LocationClient.KEY_LOCATION_CHANGED in the intent extras is always null.

Intent callbackIntent = new Intent(context, OnLocationAvail.class);
callbackIntent.putExtra(SOME_KEY, PARCELABLE_DATA);
PendingIntent.getService(context, 0, callbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem was when the LocationClient pushes an update via the callback PendingIntent, the process doesn't has any information of the ClassLoader since it is happening outside the application context and in a different process.

The following link helped me to narrow down the issue. https://code.google.com/p/android/issues/detail?id=6822

Solution:

Use a hack bundle when requesting for an location update.

Bundle bundle = new Bundle();
bundle.putParcelable("com.foo.parcel", some_parcel);
callbackPendingIntent.putExtra("com.foo.bundle",bundle);

When receiving the location update.

Bundle oldBundle = intent.getBundleExtra("com.foo.bundle");      
some_parcel = oldBundle.getParcelable("com.foo.parcel");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...