TelephonyManager crashing on android studio

我怕爱的太早我们不能终老 提交于 2019-12-11 03:28:48

问题


I have shifted my eclipse code to android studio and I tried executing the project on studio and it crashes on TelephonyManager. I have created the old code on Theme.Holo.Light .

I have added permissions:

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


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Class:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        final String tmDevice, tmSerial, androidId;
        if (tm.getDeviceId() != null) {
            tmDevice = "" + tm.getDeviceId();
            tmSerial = "" + tm.getSimSerialNumber();
            androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
            UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());

            String appdeviceId = deviceUuid.toString();

            Toast.makeText(getBaseContext(),appdeviceId,Toast.LENGTH_LONG).show();
        }
    }


}

Logcat:

java.lang.RuntimeException: Unable to start activity ava.lang.SecurityException: Neither user 10101 nor current process has android.permission.READ_PHONE_STATE.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.SecurityException: Neither user 10101 nor current process has android.permission.READ_PHONE_STATE.
            at android.os.Parcel.readException(Parcel.java:1546)
            at android.os.Parcel.readException(Parcel.java:1499)
            at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:3692)
            at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:624)
            at example.com.telephoney_manager_test.MainActivity.onCreate(MainActivity.java:24)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)

回答1:


I had this topic with Android Studio (1.4 Beta 4) and a Nexus 6 running an Android M preview.

I could fix the problem by running the app once (receiving an exception), then going to Settings -> Apps -> My app -> Permissions which showed (Surprise, surprise!) all requested permissions and all permissions were turned off.

I then turned on all permissions manually and started the app from Android Studio again. Since then the permission problem has disappeared.




回答2:


If you're targeting API 23, then you need to request any runtime permissions, which from your manifest are:

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

This is true every time you reinstall your app, which can be tedious.

Your options:

1) Install with the "-g" option. This will grant all permissions you ask for that you are reasonably allowed to have.

$ adb install -g myapp.apk

2) Grant the permissions you need at runtime using the PackageManager:

$ adb shell pm grant com.myapp "android.permission.READ_PHONE_STATE"



回答3:


Add this permission in manifest

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



回答4:


So you already mentioned in your question you already have the <uses-permission android:name="android.permission.READ_PHONE_STATE" /> permission. It means does n't happen !!

I think unfortunately your android studio is unable to build the project properly. it fails to refresh the manifest to load the permission.

So, Try to rebuilding your project (clean, build). If it doesn't help, you can try this a bit weird solution from here that seem to be working.

As a Note Check the entire manifest file. Some package name changes or misplaced data/names may causes this error.




回答5:


I had pasted the permissions below the application tag . I pasted the permissions above application tag and everything is working fine.




回答6:


If you're targeting API 23, then you need to request any runtime permissions. Here is a workaround for the issue: change the file of build.gradle, change the value of targetSdkVersion below 23.



来源:https://stackoverflow.com/questions/32332878/telephonymanager-crashing-on-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!