Daniel Sagayaraj
Daniel Sagayaraj

Reputation: 317

how to check whether boot receiver is working?

I created a boot receiver class:

public class BootReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
         Log.d("Boot receiver","Working")
    }
}

My manifest file:

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

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

I used the adb command - am broadcast -a android.intent.action.BOOT_COMPLETED

Still i cant be sure whether it is working.How can i test it?

Upvotes: 1

Views: 1675

Answers (2)

matty357
matty357

Reputation: 637

I think you have missed the @Override for on receive.

@Override
public void onReceive(Context context, Intent intent) {
 Log.d("Boot receiver","Working")
}

in my manifest i have;

 <receiver
    android:name=".BootUpReceiver"
    android:enabled="true"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
     <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />    
       <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
 </receiver>

and;

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

in BootUpReceiver.java i have;

public class BootUpReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent i = new Intent(context, Main.class);  
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);  
    }    
}

You should just be able to see your Log in the logcat, try using System.out.println("BOOT RECEIVED"); instead of Log

Upvotes: 1

EvZ
EvZ

Reputation: 12179

Keep in mind that packages have different states (Major changes came from Android 3.1).
If the user is not opening the app by himself (at least once) than your package will not receive this intent.

Otherwise it will be really easy to run some malicious code without user interaction.

As you can imagine for security reasons. There you can find more information: Trying to start a service on boot on Android

Unless you will place your APK in /system/apps folder and will sign it with system certificate.

P.S.: There is some flag that you can pass with the intent, might be interesting for you FLAG_INCLUDE_STOPPED_PACKAGES

Upvotes: 0

Related Questions