Manikandan
Manikandan

Reputation: 1519

Need to run my app before the unlock screen in android

I need to run my app as the user clicks on the unlock screen. I have used ACTION_USER_PRESENT Intent in a BroadcastReceiver to check. I used the following code. I need to show my app before the unlock screen. But my app is visible after I unlock the screen. Any help will be appreciated.

My BroadcastReceiver

package com.progressindicator;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.WindowManager;

public class Receive extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (intent.getAction() != null) {
            if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {

                Intent s = new Intent(context, ProgressIndicator.class);
                s.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                // s.setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

                s.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                context.startActivity(s);
            }
        }
    }
}

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.progressindicator"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.progressindicator.ProgressIndicator"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".Receive" >
            <intent-filter
                android:enabled="true"
                android:exported="false" >
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

My Activity:

package com.progressindicator;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ProgressIndicator extends Activity {
    ImageView loading;
    AnimationDrawable loadAnimation;
    private Window wind;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_progress_indicator);
    //  requestWindowFeature(Window.FEATURE_NO_TITLE);
    //  requestWindowFeature(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
//      wind = this.getWindow();
//      wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
//      wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
//      wind.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

        getWindow().addFlags(

                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 

            );
        LinearLayout main = new LinearLayout(this);
        main.setOrientation(LinearLayout.VERTICAL);
        setContentView(main);

        loading = new ImageView(this);
        loading.setImageResource(R.drawable.loading);
        loadAnimation = (AnimationDrawable)loading.getDrawable();
        main.addView(loading);
    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus){

    loadAnimation.start();

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.progress_indicator, menu);
        return true;
    }

}

Upvotes: 2

Views: 3285

Answers (3)

JacksOnF1re
JacksOnF1re

Reputation: 3547

You need to add WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED to your window flags, as in Activity.getWindow().addFlags(...). I recommend to use this in addition with FLAG_WATCH_OUTSIDE_TOUCH and call it in your onCreate lifecycle method.

To the "security hole" topic. It is correct that as an app developer you are able to pop up before the security lockscreen appears, but a simple home button tap will pause your activity (even a System-Alert) and show the secure guard.

I know that there are some hacky ways around this, but they do not work with every device, are somewhat broken on HTC and Samsumg and no app should be able to lock out the user.

Steve

Upvotes: 0

ali.j
ali.j

Reputation: 11

Try adding WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED for your activity to be started, this will temporarily disable keyguard/keylock as long as your activity is on top of all other windows.

Upvotes: 1

Marcio Covre
Marcio Covre

Reputation: 4556

I used this for a project some time ago

// set flags for staying on and lockscreen
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
       | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

Also I would check your Intent parameters to make sure your creating a new Activity and onCreate is called.

If you want to disable the lock screen, you will need to be a Device Administrator.

EDIT:

ACTION_USER_PRESENT is sent after the user unlocks the screen, that's why it doesn't works for showing a screen on top of the lockscreen.

You should change to ACTION_SCREEN_ON.

Edit 2: On the manifest I have android:launchMode="singleTask" and in the intent I call the activity with:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

Upvotes: 0

Related Questions