Start application when unlocking screen - Android Studio -SOLVED

Asked

Viewed 173 times

-2

I have an android app that runs in the background. It happens that the lock/unlock the mobile screen, the application stops. I wonder if there is a way to start the application as soon as the user unlock the screen. I am using the BootReceiver.

Class:

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

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

}
  • You managed to solve with my answer?

1 answer

0

Try to use it like this:

public class BootReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println(intent.getAction());
        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT))
        {
            Intent i = new Intent(context,MeuServico.class); 
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        
            context.startActivity(i);
        }
    }

Remember to declare in the Manifest:

<receiver android:name=".BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
</receiver>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.