Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 239
Y.L. Avatar asked Nov 26 '11 08:11

Y.L.


People also ask

How does broadcast receive work?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

What is a system broadcast?

System broadcasts A system broadcast is a message that the Android system sends when a system event occurs. System broadcasts are wrapped in Intent objects. The intent object's action field contains event details such as android.

What does receive boot broadcast mean?

A broadcast receiver is another important component of the Android system. Broadcast receivers are registered for specific events to occur. When the event occurs, the receiver gets invoked and performs tasks, such as showing a message to the user.


2 Answers

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>
like image 163
Lalit Poptani Avatar answered Nov 10 '22 17:11

Lalit Poptani


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.

like image 42
Yahia Avatar answered Nov 10 '22 18:11

Yahia