自定义锁定屏幕应用程序中的android锁定问题
我构建了自定义锁定屏幕应用程序,该应用程序使用广播接收器和服务来侦听用户何时打开或关闭屏幕,并从那里启动我的活动。该活动应该完全取代锁定屏幕。为了做到这一点,我的应用程序应该禁用Android股票锁,以便我的应用程序可以作为新的锁定屏幕。自定义锁定屏幕应用程序中的android锁定问题
取而代之的是,一旦应用程序第一次安装,服务首次启动应用程序似乎正在工作。当用户首次关闭手机的屏幕时,他们将会看到我的应用程序在顶部运行,并且能够使用我的应用程序解锁他们的手机。但是如果用户按下home按钮,那么一旦进入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();
基本清空当按下时。
我试图编译你的代码,并得到了你说的同样的错误。我试图修改它,使其工作,并最终得到了问题!
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()
方法.... Android转储应用程序,当它需要内存...我认为finish()
可能是帮助Android捣毁应用程序(这可能是你的原因问题随机发生)
不是从我的应用程序中,而不是从android操作系统中唯一的工作? – Peter 2012-07-21 22:46:35
对不起,我不明白你说“从Android OS” – jaisonDavis 2012-07-22 03:24:57
我刚刚试了一下,现在它不会立即返回到锁屏,但下一次我解锁设备不会去我的应用程序是什么精神疾病调出默认解锁器 – Peter 2012-07-23 15:53:51
锁定键相关逻辑的行为因设备而异。这是因为锁屏通常是由设备制造商定制的(即没有库存),其中一些尊重您使用的键盘锁逻辑,有些则不。
而且,据我所知,控制键盘锁的新方法是使用窗口标志:
// inside activity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
这虽然不会解决问题,设备仍然有他们关于这个发言权。
E.g.根据我的经验,Galaxy Nexus会在keyguard上方显示您的活动窗口,但不会忽略它(您认为Google品牌的设备应该尊重该标志,呃),所以如果您在活动中点击后退按钮 - 您将获得标准锁屏 - 而HTC One X似乎正确处理解散部分:您的活动窗口将导致标准锁屏按预期被解雇。
我发现没有办法强制所有设备正常工作。 Android API无意让您创建自定义锁定屏幕(至少目前不是)。看看商店里的人 - 他们都有不完全稳定的问题。
由于戴安娜Hackborn says in this Google Groups answer,任何你能在这方面做的是一个黑客这样期待它不时打破时间。
这可能是因为该应用程序被设置....不此问题发生连续?? – jaisonDavis 2012-07-21 15:27:19