问题
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:
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>
回答1:
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).
回答2:
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.
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.
回答3:
Are you registering your Receiver inside the code??. If not you will have to register your receiver in the manifest.xml.
回答4:
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.
来源:https://stackoverflow.com/questions/10150267/use-broadcast-receiver-to-catch-incoming-call-onreceive-not-triggered