Android 使用广播接收器来接听来电,onReceive 没有触发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10150267/
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
use Broadcast Receiver to catch incoming call, onReceive not triggered?
提问by Taxi Noi Bai Ha Noi
I want to implement a simple BroadcastReceiver, it gets triggered when there's an incoming call. However, it seems onReceive is never triggered (I checked the LogCat very carefully, no output there), what was wrong? Tks My class:
我想实现一个简单的 BroadcastReceiver,它在有来电时被触发。但是,似乎 onReceive 从未被触发(我非常仔细地检查了 LogCat,那里没有输出),出了什么问题?我的班级:
public class MyPhoneReceiver extends BroadcastReceiver {
private static final String TAG = "DEBUG";
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "Test loggiiiiiiiiiiiiiiiiiiiiiiiiing!");
}
}
My manifest file:
我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tung.le.android.receiver.phone"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
</manifest>
回答by t-gao
you may need to add android:enabled="true"to your tag in manifest, or you have to register it somewhere(e.g. the custom "YourApplication" class).
您可能需要将android:enabled="true"添加到清单中的标记,或者您必须在某处注册它(例如自定义“YourApplication”类)。
回答by richard
the problem i experienced with this BroadcastReceiver solution, is that it can not trigger any calls that happen during an active call. when i talk with a person on the phone and someone calls me on the other line (and i accept that second call), there is actually no state that changed. the phone was offhook before and it remains offhook when i accept the second call.
我在使用此 BroadcastReceiver 解决方案时遇到的问题是,它无法触发在活动呼叫期间发生的任何呼叫。当我在电话上与一个人交谈并且有人在另一条线路上给我打电话时(并且我接受了第二个电话),实际上没有任何状态发生变化。电话以前摘机,当我接听第二个电话时,它仍然处于摘机状态。
the only solution i found so far is to register an observer for android.provider.Calls and then get the newest entry from it after each data change. if someone knows a better solution, please let me know.
到目前为止,我找到的唯一解决方案是为 android.provider.Calls 注册一个观察者,然后在每次数据更改后从中获取最新条目。如果有人知道更好的解决方案,请告诉我。
回答by Vahid Ghadiri
You should at least have one activity in your project. Even if you finish it as soon as your app starts. It does not have to do anything, but it should starts at least once after installation.
您的项目中至少应该有一项活动。即使您在应用程序启动后立即完成它。它不需要做任何事情,但它应该在安装后至少启动一次。
回答by sachinr
Are you registering your Receiver inside the code??. If not you will have to register your receiver in the manifest.xml.
你在代码中注册你的接收器吗??。如果没有,您将必须在 manifest.xml 中注册您的接收器。