我正在尝试制作自定义锁定屏幕应用,但我不确定我是否会以正确的方式进行操作。我有一个广播接收器,可以在屏幕打开时收听并启动我的锁屏活动。此接收器在服务中注册,该服务还会禁用默认锁定屏幕。
问题是,屏幕开启和锁定屏幕活动显示之间有一点延迟。我该怎么做才能立刻出现?
我的服务代码:
@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver powerReceiver = new PowerReceiver();
registerReceiver(powerReceiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Service.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
和接收者:
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Intent showScreen = new Intent(context, LockScreen.class);
showScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(showScreen);
}
}