2

I want create an app that works like that:

When I take my device, I will unlock it and this app will be alredy opened. Then this app will show a compromise term of use the tablet. There will be two options, allow or refuse. If I choose allow, the app will finish; when I choose refuse, nothing happens until I choose allow.

Then, after I use my device, it will lock, then... I will unlock, and the app will be there again!

How can I put this app to auto-run when I unlock the device?

EboMike
  • 76,846
  • 14
  • 164
  • 167
David Mosiah
  • 29
  • 1
  • 1
  • 2
  • you realize there will probably not be a way for you to completely prevent the users from pressing the home button or something equivalent thereby bypassing your app altogether... – jkhouw1 Apr 28 '11 at 01:51
  • possible duplicate of [android unlock screen intent?](http://stackoverflow.com/questions/2803069/android-unlock-screen-intent) – EboMike Mar 01 '12 at 02:40

2 Answers2

5

Add the receiver in menifest file

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

Create a broadcast receiver which works to open app when phone is unlocked.

public class ScreenReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println(intent.getAction());
        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT))
        {
            Intent intent1 = new Intent(context,MainActivity.class); 
            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        
            context.startActivity(intent1);
        }
    }
Naresh Sharma
  • 4,323
  • 7
  • 48
  • 68
4

Already asked, and answered: android unlock screen intent?

You'll need to listen for that intent and then you can launch your app.

Community
  • 1
  • 1
Femi
  • 64,273
  • 8
  • 118
  • 148