android - 无法在 ICS 中接收广播

标签 android broadcastreceiver android-3.0-honeycomb android-4.0-ice-cream-sandwich

我为 Android 4.0 编写了 1 个应用程序,它是通过 broadcastreceiver 启动的。我的代码如下:

在 AndroidManifest.xml 中:

 <uses-permission android:name="android.permission.INTERNET" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name="com.Android.Exercise.StartUpReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <!--<action android:name="StartInstrument" /> 
                <action android:name="PrintControlName" />      -->     
            </intent-filter>
        </receiver>         
        <service android:enabled="true" android:name="StartAUT_Service">
            <intent-filter>
                <action android:name="com.Android.Exercise.StartAUT_Service" />
            </intent-filter>
        </service>  
    </application>

在 StartUpReceiver 类中:

public class StartUpReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i("Broadcast", "onReceive");

        Intent i = null;

        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
             i = new Intent(context, StartAUT_Service.class);          
             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        context.startService(i);            
    }       
}

在我重新启动设备后,我无法接收广播。 请帮帮我,万分感谢

最佳答案

从Android 3.0开始,引入了应用状态。 现在安装的所有应用程序都将状态设置为非 Activity 状态,使其所有 BroadcastReceivers 都处于非 Activity 状态。

要使状态处于 Activity 状态,用户必须至少启动应用程序一次,操作系统将自动激活应用程序及其所有 BroadcastReceivers。

在您的情况下,您需要添加一个 Activity ,无论是许可协议(protocol)还是帮助页面。这将为您的应用程序创建一个图标,供用户单击并激活该应用程序。

请注意,如果用户强制关闭应用程序,应用程序的状态将被设置为非 Activity 状态。

我遇到了类似的问题:Android ICS not receiving data sms

我希望这能解决您的问题或至少让您走上正轨。

问候, 艾哈迈德

关于android - 无法在 ICS 中接收广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9695514/

相关文章:

android - 广播接收器 - 监控电池电量和充电状态

menu - Android 3.0蜂窝: How to enable/disable Menu Items in Action Bar?

android - 如何更改Android溢出菜单图标

java - 听起来不行吗? (仍然需要一个答案)

android - 分区存储 - 下载文件夹访问

android - 使用 LocalBroadcastManager 时出现 NullPointerException

android - 旋转时保留 Fragment 对象

android - 用于理解 Android 概念的推荐资源

android - Flutter - Image.network 失败时的默认图像

android - Google Cloud Messaging错误: Unable to instantiate receiver