我正在尝试制作一个自定义锁屏应用程序,但我不确定我是否以正确的方式进行。我有一个广播接收器,它会在屏幕打开并开始我的锁定屏幕活动时进行监听。此接收器在服务中注册,该服务也禁用默认锁定屏幕。
问题是,在屏幕打开和锁定屏幕活动出现之间有一点延迟。我将如何去做,以便它立即出现?
我的服务代码:
@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);
}
}