android – 通过清单注册时,广播接收器无法正常工作

尝试在Android应用程序的清单中设置简单的广播接收器,以检测手机何时振铃并启动服务.它在呼叫进入时没有收到广播,没有日志输出,nada.我尝试了android.intent.action.PHONE_STATE操作以及清单中的TelephonyManager.ACTION_PHONE_STATE_CHANGED,对我来说都没有做任何事情.

<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>

和接收者:

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);
    }
}
}

我环顾四周,在这个帖子中:Incoming call broadcast receiver not working (Android 4.1)他们说从android 4.1开始,系统不再向没有活动的app组件发送广播……这是真的吗?

解决方法:

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?

自Android 3.1以来就是如此.在清单注册的BroadcastReceivers工作之前,必须明确地运行您的某个组件,并且典型的解决方案是拥有用户启动的活动(提供应用程序配置,帮助,许可协议等的活动).

请阅读the Android 3.1 release notes以获取更多信息.

上一篇:telephonymanager – 使用Android获取电话美国本地区号


下一篇:android – TelephonyManager实例化