2016-08-17 415 views
-2

我正在使用Android手機上的應用程序,當手機收到來電時需要通知您。我使用BroadcastReceiver的方法,因爲即使應用程序未處於活動狀態,我也想獲得有關來電的通知。因此,使用TelephonyManager和PhoneStateListener的方法不適合我的需要。 所以,我的應用程序清單中適當的權限:權限拒絕READ_PHONE_STATE

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

它還恰當清單中註冊廣播接收器:

<receiver android:enabled="true" android:name=".CallReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.PHONE_STATE" /> 
      </intent-filter> 
</receiver> 

類CallReceiver實現廣播接收器,但是一旦我開始應用,其方法CallReceiver的的onReceive永遠不會被調用,永遠不會被調用。 之所以這樣,是該系統拒絕交付意向到我的廣播接收器,因爲我發現下面的日誌消息,每次電話響起:

W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to com.example.incomingcall/.CallReceiver requires android.permission.READ_PRIVILEGED_PHONE_STATE due to sender android (uid 1000) 
W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to com.example.incomingcall/.CallReceiver requires android.permission.READ_PHONE_STATE due to sender android (uid 1000) 

正如我解釋說,我已經把READ_PHONE_STATE許可清單中的,而其他此處指定的權限是無法放入清單中的系統權限。任何想法如何克服這個問題? 設備在其上,這似乎在的Nexus 6P與Android 6.0.1

+3

https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-它 – CommonsWare

回答

-2
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 expanation 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_CONTACTS); 

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