0

i have android app and its working good , but now i need to open the app automatically when android device turned on i tried many solutions but its not working ,

any idea please ?

i tried like this but not working

public class Bootup extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
    Toast.makeText(context, "Reboot completed! Starting your app!!!.", Toast.LENGTH_LONG).show();

        Intent i = new Intent(context, AutoStart.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startService(i);
    }

}
}
meladandroid
  • 49
  • 2
  • 9
  • See also https://stackoverflow.com/questions/58362719/how-can-i-start-android-application-on-device-boot. – CoolMind Oct 16 '20 at 06:47

2 Answers2

0

Per the following post:

How to start an Application on startup?

You might need a Receive_boot_completed permission. There are two answers in the post which show how to start either an activity or a service on app start.

Community
  • 1
  • 1
10101010
  • 607
  • 8
  • 24
0

When you switch on the mobile, an intent will be broadcasted. Its android.intent.action.BOOT_COMPLETED.

If you receive that intent via a broadcasted receiver then it you can detect if the device is switched on.

Broadcast Receiver:

    public class BootReceiver extends BroadcastReceiver { 

    @Override
    public void onReceive(Context aContext, Intent aIntent) { 

            // ToDo: Whatever I need at immediately after bootstrap
            Toast.makeText(aContext, "This message phone is turned on!", Toast.LENGTH_LONG).show();
            Log.e("..BOOT_ACTION_NAME...", "This message phone is turned on!");


}}

Manifest Declaration:

<receiver
        android:name="com.example.testing_demo.BootReceiver"
        android:enabled="true" 
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

Place the manifest code inside the application tag.

San
  • 2,078
  • 1
  • 24
  • 42