6

I writed a small program to catch the system broadcast BOOT_COMPLETED, but it just doesn't work:

package com.alex.app.testsysreboot;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("my_tag", "system reboot completed.......");
    }    
}

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.alex.app.testsysreboot"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name=".MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>

I closed the AVD, and then clicked the button "run" in Eclipse, and the Eclipse started a new AVD, but after the system boot, I just cannot see the log in the LogCat...

Seraphim's
  • 12,559
  • 20
  • 88
  • 129
Y.L.
  • 1,274
  • 6
  • 23
  • 39
  • you can checkout my complete example [here](http://stackoverflow.com/questions/7690350/android-start-service-on-boot/7690600#7690600) – Lalit Poptani Nov 26 '11 at 08:54
  • my program is more simple than yours. i don't want to start a service, but just output one Log using Log.i(xxxxx), it just dosen't work, i concerted my program to API level 3, then it cannot be installed on the SD card, it still does't work. could you run my program in ur environment, thanks in advance... – Y.L. Nov 26 '11 at 10:51
  • Ok I will try which version you are using 3.0 or 2.2? – Lalit Poptani Nov 26 '11 at 10:54
  • i used 2.2 first, it doesn't work, and then i changed to lower version ---1.5, but it still does not ... – Y.L. Nov 26 '11 at 11:03
  • Check my complete answer, I tested it and it works fine. Hope it works for you too. – Lalit Poptani Nov 26 '11 at 11:22
  • No problem, if you still face problem let me know. – Lalit Poptani Nov 26 '11 at 11:34

2 Answers2

9

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>
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
4

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.

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • 1
    but in the SDK documents, i found the following words: the presence of at least one filter implies that the broadcast receiver is intended to receive intents broadcast by the system or other applications, so the default value is "true". i have specified the intent-filter, so the default value should be true. after i added them, it still doesn't work......by the way,how could i know whether i have istalled the App in the SD card in an AVD?? thanks in advance.... – Y.L. Nov 26 '11 at 08:40
  • @CyberRusher added link to a working sample project... if that does not work then you have hit one of the cases I describe in my answer... – Yahia Nov 26 '11 at 08:44
  • @CyberRusher for AVD see http://arthurfmay.blogspot.com/2011/06/broadcastreceiver-bootcompleted-and.html – Yahia Nov 26 '11 at 08:47
  • ok, thank you, but i am in China, the Government's "Greate Fire Wall" just blocked me from accessing the website, Fuxx the Government.... – Y.L. Nov 26 '11 at 08:50
  • 1
    it still doesn't work, the goole SDK document says the Apps can be installed on SD card after API leve8, but what i use is only 3 .......i have checked a lot of topics on it both in Einglish and chinese, but it seems that just no body konws how to catch the BOOT_COMPLETED correctly..... – Y.L. Nov 26 '11 at 10:27