Android设备无法读取手机状态

时间:2015-11-30 15:56:43

标签: android android-permissions

在我的代码中,在阅读手机状态时发生错误。不知道为什么。这是我的清单文件:

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

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

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

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

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

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

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

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission
    android:name="com.vspf.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.vspf.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:name=".Volley.VolleyApplication"
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <activity android:name=".LoginCheckActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait" />

    <activity
        android:name=".Home"
        android:screenOrientation="portrait">

    </activity>

    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <activity
        android:name=".Feedback"
        android:screenOrientation="portrait" />

    <activity
        android:name=".AddCategory"
        android:screenOrientation="portrait">

    </activity>
    <activity
        android:name=".SportDetail"
        android:screenOrientation="portrait" />

    <activity
        android:name=".EventActivity"
        android:screenOrientation="portrait" />

    <activity
        android:name=".EventDetailActivity"
        android:screenOrientation="portrait" />

</application>

的活动:

VolleySingleton volleySingleton;
RequestQueue requestQueue;

TelephonyManager mTelephonyMgr;
String ANDROID_DEVICE_ID = null;

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

    mTelephonyMgr = (TelephonyManager) getSystemService(LoginCheckActivity.TELEPHONY_SERVICE);
    ANDROID_DEVICE_ID = mTelephonyMgr.getDeviceId();

    volleySingleton = VolleySingleton.getInstance();
    requestQueue = volleySingleton.getRequestQueue();

    sendReq();
}

logcat的

11-30 22:33:03.330 3325-3325/? E/AndroidRuntime: FATAL EXCEPTION: main
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime: Process: com.vspf, PID: 3325
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vspf/com.vspf.LoginCheckActivity}: java.lang.SecurityException: getDeviceId: Neither user 10057 nor current process has android.permission.READ_PHONE_STATE.
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:  Caused by: java.lang.SecurityException: getDeviceId: Neither user 10057 nor current process has android.permission.READ_PHONE_STATE.
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1599)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1552)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4175)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:706)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at com.vspf.LoginCheckActivity.onCreate(LoginCheckActivity.java:34)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
11-30 22:33:03.330 3325-3325/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 

我在ANDROID_DEVICE_ID = mTelephonyMgr.getDeviceId();行收到错误。

我正在使用API​​ 23进行测试。 在我的其他应用程序中,此代码适用于API 23。

1 个答案:

答案 0 :(得分:2)

如果您的设备是Android 6.0 Marshmallow,请确保您的targetSdkVersion小于23或使用以下代码来确定您是否已被授予READ_PHONE_STATE权限。

{{1}}

有关&#34;在运行时请求权限的更多信息&#34; here