使用PendingIntent获取位置始终返回Null
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PendingIntent获取位置始终返回Null相关的知识,希望对你有一定的参考价值。
在了解了最佳方法Location
后,我使用PendingIntent
获取reference to my old post。 PendingIntent
正如我所需要的那样工作。但它始终将位置返回为null。
Intent intent = new Intent(this, LocationReceiver.class);
PendingIntent locationIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationIntent);
在LocationReceiver(广播接收器类)中,
public class LocationReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("LocationReceiver", "OnReceive");
Location location = (Location) intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
Log.i("LocationReceiver", lat + ", " + lng);
} else {
Log.i("LocationReceiver", "Location is null");
}
LocationReceiver.completeWakefulIntent(intent);
}
}
附加信息,
使用GooglePlayService 8.3.0
和buildToolVersion 23.0.2
我想知道在什么情况下它返回Null!任何帮助将不胜感激!
编辑:清单
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:name="package.ApplicationClass"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".activity.SplashScreen"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".activity.MapsActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.MapsActivity" />
<activity
android:name=".activity.LoginActivity"
android:label="@string/title_activity_login"
android:theme="@style/AppTheme.NoActionBar" />
<receiver android:name=".receiver.LocationReceiver"/>
</application>
</manifest>
闪屏
public class SplashScreen extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, ResultCallback<LocationSettingsResult>,
LocationListener {
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
UPDATE_INTERVAL_IN_MILLISECONDS / 2;
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
private final static String TAG = "SplashScreen";
protected Boolean mRequestingLocationUpdates;
protected Location mCurrentLocation;
protected GoogleApiClient googleApiClient;
protected LocationSettingsRequest locationSettingsRequest;
protected LocationRequest locationRequest;
/**
* Initialize GoogleApiClient Builder to fetch required Google Services
*/
protected synchronized void buildGoogleApiClient() {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
mRequestingLocationUpdates = false;
buildGoogleApiClient();
createLocationRequest();
buildLocationSettingsRequest();
checkLocationSettings();
}
@Override
protected void onStart() {
super.onStart();
// Establish the connection to GoogleApiClient
googleApiClient.connect();
}
@Override
protected void onResume() {
super.onResume();
if (googleApiClient.isConnected() && mRequestingLocationUpdates) {
startLocationUpdates();
} else
Log.i(TAG, "onResume - Failed");
}
protected void createLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
protected void buildLocationSettingsRequest() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
locationSettingsRequest = builder.build();
}
protected void checkLocationSettings() {
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(
googleApiClient,
locationSettingsRequest);
result.setResultCallback(this);
}
@Override
public void onConnected(Bundle bundle) {
if (mCurrentLocation == null)
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
}
private void validateUserAndProcess() {
boolean isExistingUser = false;
try {
isExistingUser = (boolean) ApplicationClass.mSharedPreferenceOperation.getData(
getString(R.string.is_existing_user), SharedPreferenceOperation.BOOLEAN);
} catch (Exception e) {
e.printStackTrace();
}
if (isExistingUser) {
Intent intent = new Intent(this, MapsActivity.class);
startActivity(intent);
finish();
} else {
int currentApiVersion = Build.VERSION.SDK_INT;
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = telephonyManager.getDeviceId();
Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra(getString(R.string.intent_extra_user_imei), deviceId);
intent.putExtra(getString(R.string.intent_extra_user_android_api), currentApiVersion);
startActivity(intent);
finish();
}
}
@Override
public void onConnectionSuspended(int cause) {
// In case of connection is suspended.
switch (cause) {
case CAUSE_NETWORK_LOST:
Toast.makeText(this, "Connection Suspended due to Network Lost", Toast.LENGTH_LONG).show();
break;
case CAUSE_SERVICE_DISCONNECTED:
Toast.makeText(this, "Connection Suspended due to Service Disconnection", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this, "Connection Suspended due to Unknown Issue", Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this, "Failed to connect due to " + connectionResult.getErrorCode(), Toast.LENGTH_LONG).show();
}
@Override
public void onResult(LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// Start Location Updates
startLocationUpdates();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(SplashScreen.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
"not created.");
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// Start Location Updates
startLocationUpdates();
break;
case Activity.RESULT_CANCELED:
finish();
break;
}
break;
}
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
Intent intent = new Intent(this, LocationReceiver.class);
PendingIntent locationIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationIntent);
if (mCurrentLocation != null) {
ApplicationClass.LATITUDE = mCurrentLocation.getLatitude();
ApplicationClass.LONGITUDE = mCurrentLocation.getLongitude();
validateUserAndProcess();
}
}
/**
* Requests location updates from the FusedLocationApi.
*/
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest,
this).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
mRequestingLocationUpdates = true;
}
});
Log.i(TAG, "startLocationUpdates Invoked");
}
@Override
protected void onStop() {
super.onStop();
//googleApiClient.disconnect();
}
@Override
protected void onPause() {
super.onPause();
// Stop location updates to save battery, but don't disconnect the GoogleApiClient object.
/**if (googleApiClient.isConnected()) {
stopLocationUpdates();
}*/
}
/**
* Removes location updates from the FusedLocationApi.
*/
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
mRequestingLocationUpdates = false;
}
});
}
}
答案
第一件事是你传递错误的钥匙从额外的东西获得Location。
传递这个键:com.google.android.location.LOCATION
我正确地阅读了你的问题,我也正确地阅读了answer的建议。
我认为这可以帮助您解决问题。
请参考这个答案:CLICK HERE
以上是关于使用PendingIntent获取位置始终返回Null的主要内容,如果未能解决你的问题,请参考以下文章
位置客户端在 PendingIntent 中使用可打包的附加请求请求位置更新
来自 pendingintent 的 Android 捆绑包始终接收为 null
当应用程序处于后台时,带有 PendingIntent 的 Android FusedLocationClient 未收到位置更新