且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

即使手机处于锁定模式也会显示活动

更新时间:2023-02-26 22:05:57

我使用这些Flags没有成功实现这个功能,但是使用WakeLockKeyguardLock.以下是我的工作:

I don't succeed in achieving this feature by using these Flags, but I did succeed by using the WakeLock and KeyguardLock. Below is what I do:

public class DismissLock extends Activity {

PowerManager pm;
WakeLock wl;
KeyguardManager km;
KeyguardLock kl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Log.i("INFO", "onCreate() in DismissLock");
    pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    km=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
    kl=km.newKeyguardLock("INFO");
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "INFO");
    wl.acquire(); //wake up the screen
    kl.disableKeyguard();// dismiss the keyguard

    setContentView(R.layout.main);

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    wl.release(); //when the activiy pauses, we should realse the wakelock
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    wl.acquire();//must call this!
}

}

当然,你仍然需要在清单文件中声明权限.

Of course, you still need to declare the permission in the manifest file.

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