我正在尝试使用Google FusedLocationProvider API
创建后台位置跟踪器。我已经使用了Pending Intent并创建了一个IntentService
类。我还添加了一个通知,通知用户有关正在运行的位置跟踪器。但是,当我运行应用程序时,我看不到任何通知,而是在logcat中收到以下警告:
W/Intent: Failure filling in extras
android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.google.android.gms.location.LocationResult
at android.os.Parcel.readParcelable(Parcel.java:2077)
at android.os.Parcel.readValue(Parcel.java:1965)
at android.os.Parcel.readMapInternal(Parcel.java:2226)
at android.os.Bundle.unparcel(Bundle.java:223)
at android.os.Bundle.putAll(Bundle.java:302)
at android.content.Intent.fillIn(Intent.java:6367)
at com.android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.java:210)
at com.android.server.am.PendingIntentRecord.send(PendingIntentRecord.java:192)
at android.content.IIntentSender$Stub.onTransact(IIntentSender.java:64)
at android.os.Binder.execTransact(Binder.java:351)
at dalvik.system.NativeStart.run(Native Method)
我甚至无法确定我的PendingIntent
是否正常运作。
以下是我的IntentService
班级:
public class LocationHandlerService extends IntentService {
Location mLocation;
NotificationManager notificationManager;
public LocationHandlerService() {
super("Services.LocationHandlerService");
}
/* @Override
public void setIntentRedelivery(boolean enabled) {
super.setIntentRedelivery(true);
}
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
@Override
protected void onHandleIntent(Intent intent) {
ResultReceiver locationReceiver = intent.getParcelableExtra(Constants.RECEIVER);
if (LocationResult.hasResult(intent)) {
LocationResult locationResult = LocationResult.extractResult(intent);
mLocation = locationResult.getLastLocation();
setupNotification();
Bundle bundle = new Bundle();
bundle.putParcelable(Constants.BUNDLE, mLocation);
locationReceiver.send(Constants.RESULT_DATA_KEY, bundle);
}
}
//Setup Notification
private void setupNotification() {
final Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.addCategory("android.intent.category.LAUNCHER");
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.location_start_notification)
.setOngoing(true)
.setPriority(Notification.PRIORITY_DEFAULT)
.setContentTitle(getResources().getString(R.string.location_notification))
.setContentText("Lat: " + mLocation.getLatitude() + ", Long: " + mLocation.getLongitude())
.setContentIntent(pendingIntent);
notificationManager.notify(1, notification.build());
}
@Override
public void onDestroy() {
stopForeground(true);
}
// Creating constants to report the results of the Location Update process
public final class Constants {
public static final String PACKAGE_NAME = "com.svtech.thirdeye.thirdeye";
public static final String RECEIVER = PACKAGE_NAME + ".RECEIVER";
public static final int RESULT_DATA_KEY = 9;
public static final String BUNDLE = PACKAGE_NAME + ".BUNDLE";
}
}
任何人都可以帮忙????