Search code examples
androidandroid-activitystartupboot

Start an Activity on Phone Boot in Android


I want to start my application automatically when the phone boots. I declared a BroadcastReceiver in the manifest file.

<receiver android:name=".Autostart">  
<intent-filter>  
    <action android:name="android.intent.action.BOOT_COMPLETED" />  
</intent-filter>  

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

I made a java file for the receiver.

Autostart.java

public class Autostart extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {  

    Intent pushIntent = new Intent(context, MushTouchActivity.class); 
    pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(pushIntent);
    }
}

}

But, the application does not launch when I switch my phone on. Can anyone tell me what I am missing here ?


Solution

  • try your if statement like this:

        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
    
    
            Intent i = new Intent(context, MushTouchActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
            context.startActivity(i);
        }