Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application calling with screen lock

Tags:

android

I want to launch an application immediately after the screen gets unlocked and at the time of booting. How is it possible? What changes do I need to make?

like image 904
sunny Avatar asked Jun 18 '26 05:06

sunny


1 Answers

  1. To launch your application when the screen locks, you need to register a BroadcastReceiver for the Intent.ACTION_SCREEN_OFF Intent for more information check the example http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

  2. To launch your application at the time of boot you need a broadcast receiver that listens for the BOOT_COMPLETED action, please refer How to start an Application on startup?

First, you need the permission in your manifest:

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

Also, in your manifest, define your service and listen for the boot-completed action:

<service android:name=".MyService" android:label="My Service">
    <intent-filter>
        <action android:name="com.myapp.MyService" />
    </intent-filter>
</service>

<receiver
    android:name=".receiver.StartMyServiceAtBootReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="StartMyServiceAtBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent serviceIntent = new Intent("com.myapp.MySystemService");
            context.startService(serviceIntent);
        }
    }
}

And now your service should be running when the phone starts up.

like image 132
Soham Avatar answered Jun 19 '26 18:06

Soham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!