调用需要权限,用户可能会拒绝该权限:



我得到以下代码错误

调用需要权限,但可能会被用户拒绝:代码 应明确检查以查看权限是否可用(使用 checkPermission)或显式处理潜在的安全异常 少。。。(Ctrl+F1)

检查

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

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

法典:

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

请协助清除它!

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

对于Android 6.0及更高版本,您必须先明确请求权限,然后才能使用TelephonyManager。此外,getDeviceId 已被弃用用于 Android O所以使用如下:

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

错误由

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 ==是否为真,否则您将获得应用程序崩溃。

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

另请参阅如何在安卓中以编程方式获取设备的 IMEI/ESN?

相关内容

  • 没有找到相关文章

最新更新