gpt4 book ai didi

java - 请求 READ_PHONE_STATE 权限时应用程序因 IllegalStateException 崩溃

转载 作者:行者123 更新时间:2023-12-02 11:49:59 27 4
gpt4 key购买 nike

我需要捕获设备的 IMEI 和 IMSI,您已经知道,这需要权限“android.permission.READ_PHONE_STATE”。碰巧当出现此对话框窗口请求用户获得此权限时,我的应用程序被停止。怎么解决?

@SuppressLint("MissingPermission")
public void ListarApps(View view) throws PackageManager.NameNotFoundException {

String myDeviceModel = android.os.Build.MODEL;
String myDeviceProduct = android.os.Build.MANUFACTURER;
String myVersion = Build.VERSION.RELEASE;
System.out.println("Manufacturer: " + myDeviceProduct +
" - Model: " + myDeviceModel + " - Android: " + myVersion);

// Code For IMEI AND IMSI NUMBER ("android.permission.READ_PHONE_STATE")

final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 999;

ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);


String serviceName = Context.TELEPHONY_SERVICE;
TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);

String IMEI, IMSI;

/*IMEI = m_telephonyManager.getDeviceId();
IMSI = m_telephonyManager.getSubscriberId();

System.out.println("IMEI: " + IMEI);
System.out.println("IMSI: " + IMSI);*/
}

更新(logcat 输出):

12-24 15:31:43.787 27853-27853/com.testando.teste E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.testando.teste, PID: 27853java.lang.IllegalStateException: Could not execute method for android:onClickat android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)at android.view.View.performClick(View.java:5640)at android.view.View$PerformClick.run(View.java:22455)at android.os.Handler.handleCallback(Handler.java:751)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:154)at android.app.ActivityThread.main(ActivityThread.java:6165)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)Caused by: java.lang.reflect.InvocationTargetExceptionat java.lang.reflect.Method.invoke(Native Method)at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)at android.view.View.performClick(View.java:5640) at android.view.View$PerformClick.run(View.java:22455) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6165) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) Caused by: java.lang.SecurityException: getDeviceId: Neither user 10141 nor current process has android.permission.READ_PHONE_STATE.at android.os.Parcel.readException(Parcel.java:1684)at android.os.Parcel.readException(Parcel.java:1637)at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4684)at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:866)at com.testando.teste.MainActivity.ListarApps(MainActivity.java:49)at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5640) at android.view.View$PerformClick.run(View.java:22455) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6165) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 

更新 2(AndroidManifest.xml 文件):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testando.teste">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

最佳答案

好吧,我理解您的代码,您首先需要检查是否授予权限,然后为 getDeviceId 编写代码

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// ask user permission
} else {
// READ_PHONE_STATE permission is already been granted.
TelephonyManager mngr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
IMEI = mngr.getDeviceId();
}

您还需要重写onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission is already been granted.
TelephonyManager mngr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
IMEI = mngr.getDeviceId();
}
}

关于java - 请求 READ_PHONE_STATE 权限时应用程序因 IllegalStateException 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47962854/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com