2016-10-13 11 views
0

私は少しアンドロイドに新しいです、そして、私は数日の間、今のソリューションを探しています。私は新しい6バージョンを使用してユーザーの場所を取得しようとしています - GoogleのAPIクライアントとユーザーのアクセス許可。私はアプリをインストールするとき、何も起こらないことが承認された後、許可ダイアログが表示されます(アプリは場所のトーストを表示する必要があります)。私は、アプリケーションを閉じて、それを再び開いて、私の指先を取得するとき。最初のインストールの後でトーストは動作しません。あなたは許可が付与された後mGoogleApiClient.connect();を呼び出す必要がファイアウォールユーザーの許可後許可アンドロイド

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(); 
    } 
} 

}

答えて

0

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

yeahhh thatsそれでした。ありがとう:-) – Elidotnet

関連する問題