重新安装后,getLastLocation()始终为null

时间:2016-04-19 05:34:46

标签: android google-maps geolocation location google-api-client

我似乎遇到了一个我无法弄清楚的问题。 我正在创建一个需要Android服务的应用程序。应用程序需要不断轮询位置。 此代码已经工作了几个月,然后当我从手机上卸载它并重新安装它时,该位置突然返回null。

以下是LocationProvider类,它使用Google API客户端抓取位置:

public class LocationProvider implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

public abstract interface LocationCallback {
    public void handleNewLocation(Location location);
}

public static final String TAG = LocationProvider.class.getSimpleName();

/*
 * Define a request code to send to Google Play services
 * This code is returned in Activity.onActivityResult
 */
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

private LocationCallback mLocationCallback;
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;


public LocationProvider(Context context, LocationCallback callback) {
    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    mLocationCallback = callback;

    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)

            .setInterval(3 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(1 * 1000); // 1 second, in milliseconds

    mContext = context;
}

public void connect() {
    mGoogleApiClient.connect();
}

public void disconnect() {
    if (mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "Location services connected.");
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    if (location == null) {
        //----------------

        //----------------
         Log.i(TAG, "couldnt get location");
        noLocationEnabledDialog();
        /*LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        mLocationCallback.handleNewLocation(location); */
    }
    else {
        /* Log.i(TAG, "couldnt get location");
        noLocationEnabledDialog(); */
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        mLocationCallback.handleNewLocation(location);
    }
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    /*
     * Google Play services can resolve some errors it detects.
     * If the error has a resolution, try sending an Intent to
     * start a Google Play services activity that can resolve
     * error.
     */
    if (connectionResult.hasResolution() && mContext instanceof Activity) {
        try {
            Activity activity = (Activity)mContext;
            // Start an Activity that tries to resolve the error
            connectionResult.startResolutionForResult(activity, CONNECTION_FAILURE_RESOLUTION_REQUEST);
        /*
         * Thrown if Google Play services canceled the original
         * PendingIntent
         */
        } catch (IntentSender.SendIntentException e) {
            // Log the error
            e.printStackTrace();
        }
    } else {
        /*
         * If no resolution is available, display a dialog to the
         * user with the error.
         */
        Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
    }
}

public void noLocationEnabledDialog(){
        AlertDialog.Builder setLocationDialog = new AlertDialog.Builder(mContext);
        setLocationDialog.setTitle(R.string.location_message_title);
        setLocationDialog.setMessage(R.string.location_message);

        /*Button takes user to settings*/
        setLocationDialog.setPositiveButton(R.string.affirmative, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(settingsIntent);
            }
        });
        /*If no location, close the activity*/
        setLocationDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //do nothing....yet
            }
        });
    setLocationDialog.show();
}

@Override
public void onLocationChanged(Location location) {
    mLocationCallback.handleNewLocation(location);
    Log.i(TAG, "New location received ");
} } 

我还尝试启用Google地图来抓取设备上的某个位置,希望它能够使用从地图抓取的位置。奇怪的是,在重新安装之前,位置图标显示在顶部栏中,它不再显示。有谁知道为什么重新安装应用程序会导致此错误?我在运行Android 6.0.1的三星Galaxy s6上运行它。

非常感谢!

1 个答案:

答案 0 :(得分:0)

好吧,看来我已经回答了我自己的问题。我是一个严肃的derp。 Marshmallow要求个别应用程序要求获得位置许可等。由于之前的安装具有不同的目标API,因此它不需要并且清单就足够了。

我把目标API降到22,就是这样。将来,我需要请求许可。这个问题很好地解答了:

Permission issues for location in android Marshmallow applicaton