Android设备无法读取手机状态 [英] Android device not able to read phones state

查看:554
本文介绍了Android设备无法读取手机状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my code error occurs while reading phone's state. don't know why. here is my manifest file:

<?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();行出现错误.

Here I am getting error at ANDROID_DEVICE_ID = mTelephonyMgr.getDeviceId(); line.

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

And i am using API 23 for testing. In my other application this code works perfectly for API 23.

推荐答案

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

If your device is Android 6.0 Marshmallow, make sure your targetSdkVersion less than 23 or use the following code to determine whether you have been granted READ_PHONE_STATE permission.

private void checkPermission() {
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(thisActivity,
                    Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Manifest.permission.READ_PHONE_STATE)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

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

            // MY_PERMISSIONS_REQUEST_READ_PHONE_STATE is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

有关在运行时请求权限"的更多信息,此处.

more information about "Requesting Permissions at Run Time" here.

这篇关于Android设备无法读取手机状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆