代码从If(ActivityCompat.checkSelfPermission)段返回

时间:2016-03-27 13:39:48

标签: android android-permissions

Hello Everyone我正在使用Android Studio创建导航应用程序并在执行时收到此错误。 我的代码在if(ActivityCompat.checkSelfPermission)段中停止。在这里,我有吐司声明。我已在AndroidManifest文件中提供了COARSE_LOCATIONFINE_LOCATIONINTERNET权限。 以下代码。

    public class Second extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
//Declaring Class Objects
Location location;
LocationManager lm;
GoogleApiClient googleApiClient;
//Data type for storing value of Longitude and Latitude
private double fromLongitude;
private double fromLatitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    googleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
    getLocation();
}

private void getLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.

        Toast.makeText(this,"Issue with permission",Toast.LENGTH_LONG).show();
        return;
    }
    Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    fromLatitude = location.getLatitude();
    fromLongitude = location.getLongitude();
    Toast.makeText(this,String.valueOf(fromLatitude)+" "+String.valueOf(fromLongitude),Toast.LENGTH_LONG).show();
}

//Starting Google API Client

@Override
protected void onStart() {
    googleApiClient.connect();
    super.onStart();
}

//Stopping Google API Client
@Override
protected void onStop() {
    googleApiClient.disconnect();
    super.onStop();
}

@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}
}

这会打印"Issue with Permission",如果是段,我会干杯。

1 个答案:

答案 0 :(得分:0)

试 此

checkSelfPermission(Permission)检查是否已经授予许可。
     requestPermissions(String [] permissions,int requestCode)以请求权限      onRequestPermissionsResult(int permsRequestCode,String [] permissions,int [] grantResults)检查是否授予权限的结果。

请求这样的权限

 private void askForPermission() {
   int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.CAMERA);
   if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
       if(!shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
           showMessageOKCancel("You need to allow access to Contacts",
                   new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int which) {
                           requestPermissions(new String[]{Manifest.permission.CAMERA},REQUEST_CODE_ASK_PERMISSIONS);
                       }
                   });
           return;
       }
       requestPermissions(new String[] {Manifest.permission.CAMERA},
               REQUEST_CODE_ASK_PERMISSIONS);
       return;
   }

}

请参阅此处以详细了解http://coderzpassion.com/android-new-runtime-permissions/