Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

android - Broadcast Receiver not working when registered via manifest

Trying to setup a simple broadcast receiver in the manifest of an Android app, to detect when the phone is ringing and start a service. Its not receiving the broadcast when a call comes in, no log output, nada. I tried the android.intent.action.PHONE_STATE action and also the TelephonyManager.ACTION_PHONE_STATE_CHANGED in the manifest, neither did anything for me.

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver
        android:name="com.berrmal.calllog.CallReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

and the Receiver:

public class CallReceiver extends BroadcastReceiver {

public void onReceive(Context c, Intent i) {
    Log.d("callreceiver", "onReceive()");
    String state = i.getStringExtra(TelephonyManager.EXTRA_STATE);
    Log.d("callreceiver", state);
    if (state.equals(TelephonyManager.CALL_STATE_RINGING)) {
        Intent serviceStartIntent = new Intent(c, RecordService.class);
        serviceStartIntent.putExtra("number", i.getStringExtra("EXTRA_INCOMING_NUMBER"));
        c.startService(serviceStartIntent);
    }
}
}

I looked around, and in this thread: Incoming call broadcast receiver not working (Android 4.1) they said that as of android 4.1 the system no longer sends broadcasts to app components without an activity...is this true?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Incoming call broadcast receiver not working (Android 4.1) they said that as of android 4.1 the system no longer sends broadcasts to app components without an activity...is this true?

That has been the case since Android 3.1. Something must explicitly run one of your components before your manifest-registered BroadcastReceivers will work, and the typical solution for this is to have an activity (the same one that provides app configuration, help, license agreements, etc.) that the user launches.

Please read the Android 3.1 release notes for more.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...