通过清单注册时,广播接收器不起作用 [英] Broadcast Receiver not working when registered via manifest

查看:130
本文介绍了通过清单注册时,广播接收器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Android应用的清单中设置一个简单的广播接收器,以检测电话何时响铃并启动服务.来电时,它没有接收广播,没有日志输出,nada.我尝试了清单中的android.intent.action.PHONE_STATE操作以及TelephonyManager.ACTION_PHONE_STATE_CHANGED,都没有为我做任何事情.

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>

和接收方:

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

我环顾四周,并在此线程中:来电广播接收器不起作用(Android 4.1)他们说,从android 4.1开始,系统不再在没有活动的情况下向应用组件发送广播...

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?

推荐答案

来电广播接收器无法正常工作(Android 4.1),他们说从android 4.1开始,系统不再向没有活动的应用程序组件发送广播了.

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起作用之前,必须明确运行某些组件,而典型的解决方案是让用户进行一项活动(提供应用程序配置,帮助,许可协议等的活动).发射.

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.

请阅读 Android 3.1发行说明.

这篇关于通过清单注册时,广播接收器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆