سؤال

For the need of my application, I need to display a message on the screen even if the lockscreen is enabled, then wait 3 seconds, than I have to lock again the phone as I don't want it to make unwanted phone calls in your pockets.

First part is easy:

if (PreferenceManager.getDefaultSharedPreferences(
    getBaseContext()).getBoolean("wake", false)) {
    KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
    WakeLocker.acquire(ProtoBenService.this);
    Intent myIntent = new Intent(ProtoBenService.this,LockActivity.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    if (isKeyguardUp) {
        ProtoBenService.this.startActivity(myIntent);
    } else
    Toast.makeText(ProtoBenService.this.getBaseContext(), intention, Toast.LENGTH_LONG).show();

    WakeLocker.release();
}

With this class:

public abstract class WakeLocker {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context ctx) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "CobeIm");
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }
}

And the Activity:

public class LockActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        TextView tv = new TextView(this);
        tv.setText("This is working!");
        tv.setTextSize(45);
        setContentView(tv);

        Runnable mRunnable;
        Handler mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {
                LockActivity.this.finish();
            }
        };
        mHandler.postDelayed(mRunnable, 3 * 1000);
    }
}

So, this is nice, the phone can display my text!

The only problem comes when I want to lock again the phone, it seems that locking the phone is protected by the system...

I think that my users won't understand the Device Admin and won't be able to activate it. Is there any workaround to lock the screen without the Device Admin stuff?

هل كانت مفيدة؟

المحلول 2

I am pretty sure that you have to use the Device Admin Features to lock the screen.

    protected static void initiateDeviceLock(Context context) {
    ComponentName componentName = new ComponentName(context, MyDeviceAdminReceiver.class);
    DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
    boolean active = dpm.isAdminActive(componentName);
    Log.i(context.getClass().getSimpleName(), "Active (in initiateDeviceLock) = " + String.valueOf(active));
    if (active) {
        dpm.lockNow();
    }
}

To help the user's setup the Device Admin features you can take them to the settings page:

    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    ComponentName componentName = new ComponentName(TestActivity.this, MyDeviceAdminReceiver.class);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
    startActivityForResult(intent, CODE);

نصائح أخرى

I used the following method for locking and unlocking phone.

initializing

        KeyguardLock keyguard;
        KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        keyguard = km.newKeyguardLock("MyApp");

to unlock phone

keyguard.disableKeyguard();

to lock phone again

keyguard.reenableKeyguard();    

to turn screen on

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

and dont forget to use the following permission

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top