java 使用广播接收器拒绝权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32409741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Permission Denial With Broadcast Receiver
提问by walker_4
I am trying to create an app which enters a log message when I make an outgoing call.
我正在尝试创建一个应用程序,当我拨打电话时,该应用程序会输入日志消息。
However, when I run the code, I get a permission denial, despite the fact that I have entered in the permissions.
然而,当我运行代码时,尽管我已经输入了权限,但我得到了权限拒绝。
Denial Log:
拒绝日志:
"09-04 02:35:50.535 1294-1666/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.intent.action.NEW_OUTGOING_CALL flg=0x10000010 (has extras) } to samples.varma.packagecom.testreceive2/.CallReceiver requires android.permission.PROCESS_OUTGOING_CALLS due to sender android (uid 1000)"
“09-04 02:35:50.535 1294-1666/? W/BroadcastQueue:权限拒绝:接收 Intent { act=android.intent.action.NEW_OUTGOING_CALL flg=0x10000010(有额外的)} 到 samples.varma.package2/testre由于发件人android(uid 1000),.CallReceiver需要android.permission.PROCESS_OUTGOING_CALLS”
Manifest Code:
清单代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="samples.varma.packagecom.testreceive2" >
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:enabled="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
</application>
</manifest>
And here is the code for my receiver:
这是我的接收器的代码:
package samples.varma.packagecom.testreceive2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
public class CallReceiver extends BroadcastReceiver {
public CallReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state == null) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("TAG", "Outgoing Number: " + number);
} else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("TAG", "Incoming Number: " + number);
}
}
}
I am very new to this so there is a good chance that there are several errors or I am completely off base. Regardless I would greatly appreciate any guidance. Would anyone know why I am getting this denial?
我对此很陌生,所以很有可能有几个错误,或者我完全偏离了基础。无论如何,我将不胜感激任何指导。有谁知道我为什么会被拒绝?
Thanks
谢谢
Edit:
编辑:
It is also giving me these permission denials even though I have added the phone state permission.
即使我添加了电话状态许可,它也给了我这些许可拒绝。
The privileged phone-state permission is a system permission so I cannot add.
特权电话状态权限是系统权限,因此我无法添加。
09-04 04:36:03.249 1294-1440/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to samples.varma.packagecom.testreceive2/.CallReceiver requires android.permission.READ_PRIVILEGED_PHONE_STATE due to sender android (uid 1000)
09-04 04:36:03.271 1294-1308/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to samples.varma.packagecom.testreceive2/.CallReceiver requires android.permission.READ_PHONE_STATE due to sender android (uid 1000)
1294-1308/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to samples.varma.packagecom.testreceive2/.CallReceiver requires android.permission.READ_PHONE_STATE due to sender android (uid 1000)
采纳答案by walker_4
I got it to work by following this link closely Intercepting outgoing call - what am I missing?(thanks ajit)
我通过密切关注此链接使其工作拦截拨出电话 - 我错过了什么?(感谢阿吉特)
I ended up taking off the PHONE_STATE
permission, adding android:enabled="true"
and android:exported="true"
to my receiver in the manifest, relocating the NEW_OUTGOING_CALL
permission to below application(not sure if this is necessary), taking away the intended sdk versions and basically copying the receiver from the link.
我最终取消了PHONE_STATE
许可,在清单中添加android:enabled="true"
和添加android:exported="true"
到我的接收器,将NEW_OUTGOING_CALL
许可重新定位到以下应用程序(不确定是否有必要),带走预期的 sdk 版本并基本上从链接复制接收器。
Updated manifest code from receiver tag to manifest tag is:
从接收者标签更新到清单标签的清单代码是:
<receiver
android:name=".testreceive3"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>-->
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />>-->
</manifest>
回答by Евгений Коптюбенко
I have launched the same application on Android emulator and nothing helped, even
我在 Android 模拟器上启动了相同的应用程序,但没有任何帮助,即使
android:enabled="true"
android:exported="true"
The solution was to go to Settings->Apps -> MyApplication -> Permissions -> Toggle Phone Permission on.
解决方案是转到设置-> 应用程序-> 我的应用程序-> 权限-> 打开电话权限。
回答by ThomsonStan
U need to apply the runtime permission int your code when your code running on Android 6.0 or above.
当您的代码在 Android 6.0 或更高版本上运行时,您需要在您的代码中应用运行时权限。
回答by Hrushikesh Sawant
Best solution is when you run the code activate usb debugging, make sure you select disable permissions monitor settings in developer settings ! App wont be asked for permissions by the OS anymore. Happy helping :)
最好的解决方案是当您运行代码时激活 USB 调试,确保您在开发人员设置中选择禁用权限监视器设置!操作系统不再要求应用程序授予权限。乐于助人:)
This will work without changing anything in manifest!
这将在不更改清单中的任何内容的情况下工作!
回答by Somasundaram NP
You should add the following thing in your Manifest receiver
您应该在清单接收器中添加以下内容
<service android:name=".CallReceiver"
android:enabled="true"
android:exported="false" >
</service>
回答by koutuk
Use permission CALL_PHONE
instead of OUTGOING
使用权限CALL_PHONE
代替OUTGOING
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />