2015-11-30 29 views
0

在阅读手机状态时发生我的代码错误。不知道为什么。这里是我的清单文件:Android设备无法读取手机状态

<?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

请包含相关的logcat消息。 –

+1

你的'targetSdkVersion'是什么?你在测试什么版本的Android? – CommonsWare

+0

另外,您的清单结构可能有问题。请张贴整个清单,而不仅仅是行。如果您不在''标签外部放置这些标签,它们将被忽略。 –

回答

2

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

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 
    } 
} 

有关“在运行时请求权限”的更多信息here

+0

什么是MY_PERMISSIONS_REQUEST_READ_PHONE_STATE在这里? –

+0

@chiragpatel MY_PERMISSIONS_REQUEST_READ_PHONE_STATE是一个应用程序定义的int常量。它特定的请求代码与报告给onRequestPermissionsResult(int,String [],int [])的结果相匹配。 – Chaos

+0

谢谢。但我收到未定义的错误MY_PERMISSIONS_REQUEST_READ_PHONE_STATE。 ! –