2017-11-09 3 views
0

私は修正カントと私はALT +起こる 何も入力しないを押したときに、私はすでに マニフェストにpermission.ACCESS_FINE_LOCATIONなどを追加して、誰かが私を助けてくださいことができますか?エラーが発生していますか私はエラー

private void startLocationUpdates(){ 
      String[] PERMISSIONS = {android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION}; 

      if (ContextCompat.checkSelfPermission(this, PERMISSIONS[0]) 
        != PackageManager.PERMISSION_GRANTED || 
        ContextCompat.checkSelfPermission(this, PERMISSIONS[1]) 
          != PackageManager.PERMISSION_GRANTED 
        ) { 
    //gives me error in this activity.compat.request 
       ActivityCompat.requestPermissions((Activity) this, PERMISSIONS, 0); 

      } else { 

       LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);// and error in this line too 
       Toast.makeText(this, "sinal pedido", Toast.LENGTH_SHORT).show(); 
      } 

     } 
+0

マニフェストに 'ACCESS_COARSE_LOCATION'パーミッションを含めることをお勧めします – mrid

+0

あなたは何のエラーが発生していますか? – mrid

+0

http://prntscr.com/h8csqr –

答えて

0

次に、あなただけの呼び出すためにあなたのonResume()オーバーライドを変更します。この

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 

public boolean checkLocationPermission() { 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 

      // Show an explanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 
      new AlertDialog.Builder(this) 
        .setTitle(R.string.title_location_permission) 
        .setMessage(R.string.text_location_permission) 
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          //Prompt the user once explanation has been shown 
          ActivityCompat.requestPermissions(MainActivity.this, 
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
            MY_PERMISSIONS_REQUEST_LOCATION); 
         } 
        }) 
        .create() 
        .show(); 


     } else { 
      // No explanation needed, we can request the permission. 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
     } 
     return false; 
    } else { 
     return true; 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_LOCATION: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // location-related task you need to do. 
       if (ContextCompat.checkSelfPermission(this, 
         Manifest.permission.ACCESS_FINE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED) { 

        //Request location updates: 
        locationManager.requestLocationUpdates(provider, 400, 1, this); 
       } 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 

      } 
      return; 
     } 

    } 
} 

を試してみて動作するようです 何もなく、すべてを試みcheckLocationPermission()

@Override 
protected void onResume() { 
    super.onResume(); 

    if (checkLocationPermission()) { 
     if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 

      //Request location updates: 
      locationManager.requestLocationUpdates(provider, 400, 1, this); 
     } 
    } 

} 

出典:this

関連する問題