权限android后的fireup用户位置

时间:2016-10-13 13:32:12

标签: android

我对android有点新意,我现在正在寻找几天的解决方案。我正在尝试使用新的6版本获取用户位置 - Google api客户端和用户权限。当我安装应用程序时,我看到权限对话框,在批准后没有任何事情发生(应用程序需要显示位置吐司)。当我关闭应用程序并再次打开它的工作,我得到我的坐标。只有在第一次安装后,吐司才能正常工作。

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {


private static int REQUEST_CODE_RECOVER_PLAY_SERVICES = 200;

private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private LocationRequest mLocationRequest;

private static final int MY_PERMISSION_REQUEST_LOCATION = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

   FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });


    if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_REQUEST_LOCATION);
    }
    else{
        Toast.makeText(this, "Step1s", Toast.LENGTH_LONG).show();
        if(checkGooglePlayServices()) {
            buildGoogleApiClient();
            createLocationRequest();
        }
    }
}



@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == MY_PERMISSION_REQUEST_LOCATION){
        if(grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            if(checkGooglePlayServices()) {
                buildGoogleApiClient();
                createLocationRequest();
            }
        }
    }
    else{
        Toast.makeText(this, "Location is not approved by user", Toast.LENGTH_LONG).show();
    }

}










private boolean checkGooglePlayServices(){
    int checkGooglePlayServices = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if(checkGooglePlayServices != ConnectionResult.SUCCESS){
        GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices, this, REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
        return false;
    }
    return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQUEST_CODE_RECOVER_PLAY_SERVICES){

        if(resultCode == RESULT_OK){

            if(!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()){

                mGoogleApiClient.connect();

            }

        }else if(resultCode == RESULT_CANCELED){

            Toast.makeText(this, "Google play service must be installed.", Toast.LENGTH_LONG).show();
            finish();
        }

    }

}
protected synchronized void buildGoogleApiClient(){

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
@Override
public void onConnected(Bundle bundle) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if(mLastLocation != null){
        Toast.makeText(this, "Latitude: " + mLastLocation.getLatitude() + ", Longitude: " + mLastLocation.getLongitude(), Toast.LENGTH_LONG).show();
    }
    startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {

}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}
@Override
protected void onStart(){

    super.onStart();
    if(mGoogleApiClient != null){
        mGoogleApiClient.connect();
    }
}
protected void startLocationUpdates(){

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

}
protected void createLocationRequest(){
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(20000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
public void onLocationChanged(Location location){

    mLastLocation = location;
    Toast.makeText(this, "Latitude: " + mLastLocation.getLatitude() + ", Longitude: " + mLastLocation.getLongitude(), Toast.LENGTH_LONG).show();
}
protected void stopLocationUpdates(){

    if(mGoogleApiClient != null){

        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

    }

}
@Override
protected void onPause(){
    super.onPause();
    stopLocationUpdates();
}
@Override
protected void onStop(){
    super.onStop();
    if(mGoogleApiClient != null){
        mGoogleApiClient.disconnect();
    }
}

}

1 个答案:

答案 0 :(得分:0)

在获得许可后,您需要致电mGoogleApiClient.connect();

if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    if (checkGooglePlayServices()) {
        buildGoogleApiClient();
        mGoogleApiClient.connect();
        createLocationRequest();
    }
}