通话需要许可,但可能会被用户拒绝:

时间:2019-01-24 08:46:17

标签: android android-studio android-permissions android-securityexception

我低于代码错误

  

此呼叫需要许可,但可能会被用户拒绝:代码   应该显式检查以查看权限是否可用(带有   checkPermission)或显式处理潜在的SecurityException   更少...(Ctrl + F1)

检查信息:此检查将扫描您的代码和库,并查看正在使用的API,并根据访问这些API所需的权限集进行检查。如果使用这些API的代码在运行时被调用,则程序将崩溃。

此外,对于可撤消的权限(使用targetSdkVersion 23),如果用户在运行时拒绝许可请求,则还必须准备客户端代码来处理引发异常的调用。问题ID:MissingPermission

代码:

@SuppressWarnings( "deprecation" )
    void setDeviceImei() {
        TelephonyManager telephonyManager = ( TelephonyManager ) getSystemService( Context.TELEPHONY_SERVICE );
        Utils.setIMEI( context, String.valueOf( telephonyManager.getDeviceId() ) );
        WifiManager wm = ( WifiManager ) getApplicationContext().getSystemService( WIFI_SERVICE );
        String ip = Formatter.formatIpAddress( wm.getConnectionInfo().getIpAddress() );
        Utils.setIpAddress( context, ip );
        int v = 0;
        try {
            v = context.getPackageManager().getPackageInfo( context.getPackageName(), 0 ).versionCode;
            Utils.setVersionCode( context, v );
        }
        catch( NameNotFoundException e ) {
            e.printStackTrace();
        }
        if( !Utils.isConnectingToInternet( context ) ) {
            alertpopup( context, context.getResources().getString( R.string.no_internet ) );
            return;
        } else {
            timer = new Timer();
            timer.schedule( new TimerTask() {
                public void run() {
                    callValidateImeiApi();
                    timer.cancel();
                    timer = null;
                }
            }, 500 );
        }
    }

以下行中的错误:

Utils.setIMEI( context, String.valueOf( telephonyManager.getDeviceId() ) );

请协助清除它!

3 个答案:

答案 0 :(得分:1)

要获取设备ID,您必须在清单中添加以下行 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

对于Android 6.0及更高版本,必须先明确请求权限,然后才能使用TelephonyManager。 此外,Android O不建议使用getDeviceId 因此使用如下:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  
      //getDeviceId() is Deprecated so for android O we can use getImei() method  
      return telephonyManager.getImei();  
}  
else {  
      return telephonyManager.getDeviceId();  
}

答案 1 :(得分:1)

错误是由

生成的
telephonyManager.getDeviceId()

首先,您需要添加AdroidManifest.xml:

<uses-permission android:name="Manifest.permission.READ_PHONE_STATE" />

从SDK 24开始,必须在运行时(应用程序启动时)询问权限,然后应用程序将弹出一个对话框询问权限。

详细说明在这里。 https://developer.android.com/training/permissions/requesting

您应在应用中做什么:

// couple of members to be added in the Activity

//identify for what permission we ask
private static final int WANT_TO_READ_PHONE_STATE =1;
//flag to rememebr if permission granted or not, default is false
private boolean READ_PHONE_STATE_granted = false;


/* ask for permissions */
private void askForPermission(String permission, Integer requestCode) {
    if (Build.VERSION.SDK_INT >= 23 &&
            ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {

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

            //This is called if user has denied the permission before
            //In this case I am just asking the permission again
            ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);

        } else {
            /* if permission was requested in AndroidManifest.xml then it is automatically granted for SDK <=23 */
            ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
        }
    } else {
        // on
        READ_PHONE_STATE_granted = true;

    }
}


/* Callback when user agreed/disagreed */
@Override
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == WANT_TO_READ_PHONE_STATE) {
        // check only the results
        for( int i=0; i< grantResults.length; i++) {
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                READ_PHONE_STATE_granted = true;

            }
        }
    }
}


// ask for permissions somwhere in onCreate
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    askForPermission(Manifest.permission.READ_PHONE_STATE, WANT_TO_READ_PHONE_STATE);
    /*... other code */
}

现在,当调用getDeviceId时,应该首先检查READ_PHONE_STATE_granted == true,否则会导致应用程序崩溃。

答案 2 :(得分:0)

telephonyManager.getDeviceId()在API级别26中不推荐使用此方法 https://developer.android.com/reference/android/telephony/TelephonyManager#getDeviceId()

试试看

@RequiresApi(api = Build.VERSION_CODES.O)
 TelephonyManager tm = (TelephonyManager)
            getSystemService(this.TELEPHONY_SERVICE);
    String imei = tm.getImei();

另请参见How to get the device's IMEI/ESN programmatically in android?