【发布时间】:2015-08-31 15:24:09
【问题描述】:
我无法使用我的PendingIntent 发送额外的字符串,我将其传递给LocationServices.FusedLocationApi.requestLocationUpdates(GoogleApiClient client, LocationRequest request, PendingIntent callbackIntent)。
我在Intent 上添加的用户名似乎正在破坏requestLocationUpdates 试图移交给我的IntentService 的位置,因为intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED) 返回null。
编辑
我尝试创建一个实现Parcelable 的User 类并将其作为额外的:
mRequestLocationUpdatesIntent.putExtra("username", new User(username));
我还尝试将Parcelable User 放入Bundle 中,正如此错误报告https://code.google.com/p/android/issues/detail?id=81812 中的评论所建议的那样:
Bundle userBundle = new Bundle();
userBundle.putParcelable("user", new User(username));
mRequestLocationUpdatesIntent.putExtra("user", userBundle);
为我服务:
Bundle userBundle = intent.getBundleExtra("user");
User user = userBundle.getParcelable("user");
String username = user.getUsername();
但是,这些方法都没有任何区别。每当我在意图中添加任何额外内容时,更新发生时该位置永远不会添加到意图中。
我设置了这个IntentService 来处理位置更新:
public class LocationUpdateService extends IntentService {
private final String TAG = "LocationUpdateService";
public LocationUpdateService() {
super("LocationUpdateService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent");
Bundle extras = intent.getExtras();
Log.d(TAG, "keys found inside intent: " + TextUtils.join(", ", extras.keySet()));
String username = intent.getStringExtra("username");
if (username != null) {
Log.d(TAG, "username: " + username);
} else {
Log.d(TAG, "username: null");
}
if (!intent.hasExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED)) {
Log.d(TAG, "intent does not have location :(");
}
Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
if (location == null) {
Log.d(TAG, "location == null :(");
}
Log.d(TAG, "latitude " + String.valueOf(location.getLatitude()));
Log.d(TAG, "longitude " + String.valueOf(location.getLongitude()));
...
}
}
当用户点击一个按钮时,startLocationUpdates 在我的主要活动中被调用:
主要活动类:
...
Boolean mLocationUpdatesEnabled = false;
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(LOCATION_UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(LOCATION_UPDATE_FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
protected void startLocationUpdates() {
Log.d(TAG, "startng location updates...");
mLocationUpdatesEnabled = true;
if (mLocationRequest == null) {
createLocationRequest();
}
// create the Intent to use WebViewActivity to handle results
Intent mRequestLocationUpdatesIntent = new Intent(this, LocationUpdateService.class);
// create a PendingIntent
mRequestLocationUpdatesPendingIntent = PendingIntent.getService(getApplicationContext(), 0,
mRequestLocationUpdatesIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
// request location updates
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest,
mRequestLocationUpdatesPendingIntent);
Log.d(TAG, "location updates started");
}
protected void stopLocationUpdates() {
Log.d(TAG, "stopping location updates...");
mLocationUpdatesEnabled = false;
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient,
mRequestLocationUpdatesPendingIntent);
Log.d(TAG, "location updates stopped");
}
这一切都很好,很好;当用户按下按钮时,toggleLocationUpdates 会被调用,LocationServices.FusedLocationApi.requestLocationUpdates 会调用我的LocationUpdateService,我可以在其中获取位置。
当我尝试使用 Intent.putExtra(String, String) 将额外的字符串添加到我的 Intent 时,问题就来了:
主要活动类:
...
protected void startLocationUpdates(String username) {
....
// create the Intent to use WebViewActivity to handle results
Intent mRequestLocationUpdatesIntent = new Intent(this, LocationUpdateService.class);
//////////////////////////////////////////////////////////////////
//
// When I put this extra, IntentService sees my username extra
// but the parcelableExtra `location` == null :(
//
//////////////////////////////////////////////////////////////////
mRequestLocationUpdatesIntent.putExtra("username", username);
...
}
...
编辑 我将下一个句子作为陈述而不是问题开始:“我正在使用......”
我是否使用正确的方法向此位置更新处理 IntentService 发送一些额外的数据,还是有更理智的方法来解决这个问题?
这是一个错误还是只是糟糕的文档?
【问题讨论】:
-
@BladeCoder 为我提供了一些指导,以回应我在 Android 开发 Google+ 社区中的帖子:plus.google.com/117366723702848823459/posts/6QAmns2pQCT 一旦我弄清楚了我会发布我的答案
-
一个澄清的问题,它打印出
"location == null :("- 即hasExtra()返回false,还是intent.getParcelableExtra()只是返回null? -
如果我删除 return 语句,我会得到
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference我编辑了 IntentService 以提供详细的日志记录,显示Intent.hasExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED返回 false,location == null,并引发空对象引用异常。 -
我也得到了这个日志输出
keys found inside intent: username;但是,当我没有将“用户名”放在意图上时,我会得到以下输出:keys found inside intent: com.google.android.location.LOCATION, com.google.android.gms.location.EXTRA_LOCATION_RESULT
标签: android android-intent google-play-services android-location fusedlocationproviderapi