我需要捕获设备的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:main进程:com.testando.teste,PID:27853 java.lang.IllegalStateException:无法执行android的方法:onClick在Android.sview.View.View.View $ ReadClick.run(View.java)的android.view.View.performClick(View.java:5640)上的android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) :22455)在android.os.Handler.handleCallback(Handler.java:751)的android.os.Handler.dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:154)在android .app.ActivityThread.main(ActivityThread.java:6165)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:888)at com的java.lang.reflect.Method.invoke(Native Method) .android.internal.os.ZygoteInit.main(ZygoteInit.java:778)由android.support.v7.app的java.lang.reflect.Method.invoke(Native Method)中的java.lang.reflect.InvocationTargetException引起。 AppCompatViewInflater $ DeclaredOnC lickListener.onClick(AppCompatViewInflater.java:288)在android.view.View.performClick(View.java:5640)的android.view.View $ PerformClick.run(View.java:22455)android.os.Handler.handleCallback (Handler.java:751)android.app.Handler.dispatchMessage(Handler.java:95)android.app.Looper.loop(Looper.java:154)android.app.ActivityThread.main(ActivityThread.java: 6165)位于com.android.internal.os.ZygoteInit.main的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:888)的java.lang.reflect.Method.invoke(Native Method)( ZygoteInit.java:778)引起:java.lang.SecurityException:getDeviceId:用户10141和当前进程都没有android.permission.READ_PHONE_STATE。在android.os.Parcel.readException(Parcel.java:1684)的android.os.Parcel.readException(Parcel.java:1637)at com.android.internal.telephony.ITelephony $ Stub $ Proxy.getDeviceId(ITelephony.java) :4684)在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 at android.sview.View上的android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)在android.view.View.View上查看$ PerformClick.run(View.java)的android.view.performClick(View.java:5640) 22455)在android.os.Handler.handleCallback(Handler.java:751)的android.os.Handler.dispatchMessage(Handler.java:95)android.os.Looper.loop(Looper.java:154)。 app.ActivityThread.main(ActivityThread.java:6165)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:888)at com的java.lang.reflect.Method.invoke(Native Method)。 android.internal.o s.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>
好的,因为我理解您的代码,您首先需要检查是否授予权限然后获取设备ID的wirte代码
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();
}
您还需要覆盖OnRequestionResult
@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();
}
}
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();
}