how to catch the system broadcast BOOT_COMPLETED, my program just doesn't work?

萝らか妹 提交于 2019-11-27 14:13:56

Well I tried this and it Works for me,

public class Autostart extends BroadcastReceiver 
{
    public void onReceive(Context arg0, Intent arg1) 
    {
        Log.i("Autostart", "**********started************");
    }
}

AndroidManifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pack.saltriver" android:versionCode="1" android:versionName="1.0"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <receiver android:name=".Autostart">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
Yahia

You need to add

android:enabled="true" 
android:exported="true" 

AND

make sure that the app is not installed on the SD card - IIRC apps installed there don't receive that BOOT_COMPLETED.

Another point is that devices with "Fast Boot" enabled (like several HTC devices) (sometimes?) don't send BOOT_COMPLETED.

Since Android 3.1+ there is some more weirdness regarding BOOT_COMPLETED relating to "very first start of an app" - see http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html

A working sample project with source see https://github.com/commonsguy/cw-advandroid/tree/master/SystemEvents/OnBoot

From http://arthurfmay.blogspot.com/2011/06/broadcastreceiver-bootcompleted-and.html

So instead, from Eclipse I just went into the Android SDK and AVD Manager (under the Window Menu) and started the emulator from there. I did this of course after loading the app into the emulator. I start the emulator and my BroadcastReceiver on boot works just fine. There was no need to go to running the emulator at the command line.

Another working sample can be found here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!