我构建了一个自定义锁屏应用程序,它使用广播接收器和服务来监听用户何时打开或关闭屏幕并从那里启动我的活动。该活动应该完全取代锁定屏幕。为了做到这一点,我的应用程序应该禁用 android stock lock,以便我的应用程序可以用作新的锁屏。
相反,一旦首次安装应用程序,服务首次启动,应用程序似乎正在工作。当用户重新打开手机时第一次关闭手机屏幕时,他们会看到我的应用程序在顶部运行,并且能够使用我的应用程序解锁他们的手机。但是一旦进入android操作系统,如果用户下次关闭屏幕并重新打开它而不是回到我的应用程序时按下主页按钮,他们就会被带到库存解锁屏幕,我的应用程序在它下面打开,当它应该在顶部时。
这是我的代码:
我的服务:
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("MyService","Service STARTED");
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
final BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
}
我的广播接收器:
public class ScreenReceiver extends BroadcastReceiver {
public static ArrayList<String> runningApplications = new ArrayList<String>();
private Context ctext;
public static boolean screenIsLocked;
public static KeyguardManager keyguardManager;
public static KeyguardLock lock;
@Override
public void onReceive(final Context context, final Intent intent) {
ctext = context;
keyguardManager = (KeyguardManager)ctext.getSystemService(Activity.KEYGUARD_SERVICE);
lock = keyguardManager.newKeyguardLock(Context.KEYGUARD_SERVICE);
lock.disableKeyguard();
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenIsLocked = true;
Log.d("ScreenReceiver", "False");
Intent intenti = new Intent();
intenti.setClass(context, starterActivity.class);
intenti.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intenti.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intenti);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenIsLocked = false;
Log.d("ScreenReceiver", "True");
Intent intenti = new Intent();
intenti.setClass(context, starterActivity.class);
intenti.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intenti.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intenti);
}
}
我开始的活动基本上是空的,只有一个解锁按钮finish();
在按下时调用。