【问题标题】:Why broadcast receiver is not executing BOOT_COMPLETED and not displaying toast message?为什么广播接收器不执行 BOOT_COMPLETED 并且不显示 toast 消息?
【发布时间】:2017-02-22 16:05:31
【问题描述】:
  • 我正在尝试使用 BOOT_COMPLETED 操作注册的广播接收器的简单示例...但是,应用程序没有提供 toast 消息...我搜索了很多...尝试了在 manifest.xml 和 java 中注册接收器的两种方法代码也...但我真的不知道它有什么问题...
  • 请帮我解决这个问题,因为我需要在启动时启动服务..
  • 提前致谢..

1) 活动类

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {
        Toast.makeText(context, "Boot is completed..", Toast.LENGTH_SHORT).show();
    }
} 

2) 清单.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bootupservicedemoagain"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="com.example.bootupservicedemoagain.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

我也在活动类 onReceive 方法中尝试过这个,但结果相同!

if(intent.getAction() != null) 
{
    if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
    {
        Toast.makeText(context, "Boot is completed..", Toast.LENGTH_SHORT).show();
    }
}

【问题讨论】:

    标签: android broadcastreceiver android-broadcast


    【解决方案1】:

    对您的意图过滤器尝试操作权限android.intent.action.QUICKBOOT_POWERON。它为我解决了同样的问题。您在清单文件中的接收器声明应如下所示:

     <receiver android:name="com.example.bootupservicedemoagain.BootReceiver" >
        <intent-filter>
           <action android:name="android.intent.action.BOOT_COMPLETED" />
           <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
     </receiver>
    

    另外,类别&lt;category android:name="android.intent.category.HOME" /&gt; 也不需要。

    【讨论】:

    • 相同的输出...没有显示消息
    • 你有什么设备?我也试过三星、htc、micromax 标签、模拟器……但输出相同
    • LG 擎天柱 G,Galaxy Tab 2 10.1
    【解决方案2】:

    看来我的注册和你的启动接收器注册的区别在于清单中的类别。

    <category android:name="android.intent.category.HOME" />
    

    考虑删除该行并重试。

    用我的代码编辑

    public class OnBootReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            /**
             * The onReceive method is a default method which is called when the broadcast
             * receiver is registered and the intent with the action below is sent.
             * This action will start the service. ACTION_BOOT_COMPLETED is defined in the 
             * AndroidManifest.xml.
             */
            if (Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction())) {
                Intent myIntent = new Intent(context, MyService.class);
                context.startService(myIntent);
            }
    
        }
    
    }
    

    清单

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    <!-- Handling for On Boot Receiver -->
        <receiver android:name="path.to.OnBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <!-- End Handling for On Boot Receiver -->
    
    <service
        android:name="path.to.service"
        android:enabled="true"
        android:exported="false" >
    </service>
    

    【讨论】:

    • 你有什么设备?我也试过三星、htc、micromax 标签、模拟器……但输出相同
    • Nexus 5、Moto G 和带有自定义 rom 的 HTC Desire HD。另外我刚刚意识到,如果您在广播接收器中没有上下文,那么发送 toast 消息有点困难。在服务中显示 toast 消息,然后重试。您可能有 applicationcontext 并且无法显示 toast 消息。
    • 不工作...我真的不明白问题出在哪里?因为所有代码都是正确的.. 帮助表示赞赏.. thnks
    • 当我调试这个时,我使用服务来显示 toast 消息,而不是广播接收器。每次我启动电话时,我都会收到关于所有生命周期方法(oncreate 等)的垃圾邮件。
    • 我还注意到您的清单中没有您的服务。查看我的编辑。
    【解决方案3】:

    以下组合对我有用: 在清单文件中

            <!-- Get Boot Action -->
        <receiver
            android:name="com.example.try.BootBroadcast"
            android:enabled="true"
            android:exported="true"
            android:label="MintBootReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
            <intent-filter>
                <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.HOME" />
                    <category android:name="android.intent.category.LAUNCHER" />
                    <category android:name="android.intent.category.DEFAULT" />
                </action>
            </intent-filter>
    

    对于 JAVA 广播和你所做的一样

    public class BootBroadcast extends BroadcastReceiver {
    
    // Strings - Log
    final String TAG = "BootBroadCast";
    
    @Override
    public void onReceive(Context mBootCtx, Intent mBootIntent) {
        Log.i(TAG, "BootBroadCast BroadcastReceiver +++STARTED+++");
        Log.d(TAG, "@Boot actionCaught :" + mBootIntent.getAction());
            if ("android.intent.action.BOOT_COMPLETED".equals(mBootIntent
                    .getAction())) {
                Log.d(TAG, "@Boot actionCaught :" + mBootIntent.getAction());
                // Now you are getting your Boot reciever
            }
        Log.i(TAG, "BootBroadCast BroadcastReceiver ---END/FIN---");
    }
    

    请告诉我这是否有帮助。

    【讨论】:

    • 为什么我们需要android.intent.action.ACTION_BOOT_COMPLETED
    • @IgorGanapolsky 当我测试它时,它是基于设备的,一些设备(制造商)听了 ACTION_BOOT_COMPLETED 而其他的 BOOT_COMPLETED
    【解决方案4】:

    有时接收者的完整路径会出现问题。试试相对

        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <uses-permission android:name="android.permission.QUICKBOOT_POWERON" />
        <receiver
            android:name=".BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>
    

    【讨论】:

    • 如果路径不正确,项目将无法编译。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多