尝试在 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 开始,系统不再向没有活动的应用程序组件发送广播......这是真的吗?