chirag patel chirag patel - 1 month ago 20
Android Question

Android device not able to read phones state

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>




Activity:

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) 


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

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

Answer

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.