在屏幕解锁上启动Android应用程序

时间:2012-06-23 20:07:18

标签: java android

我想构建一个锁屏更换应用程序。有没有办法创建一个只要用户唤醒/解锁屏幕就会启动我的应用程序的监听器/服务?

2 个答案:

答案 0 :(得分:7)

我相信您正在寻找ACTION_USER_PRESENT意图行动。

  

public static final String ACTION_USER_PRESENT

     

自:API Level 3   广播操作:在设备唤醒后用户出现时发送(例如,当键盘锁定消失时)。

也就是说,Android目前不支持更换锁定屏幕。市场上声称这样做的任何应用程序都会使用安全循环漏洞并且不安全。 You can read this thread for more information.(特别是,您应该阅读Mark Murphy的帖子)。遗憾。

答案 1 :(得分:7)

请参阅mylockforandroid的源代码,您需要使用DeviceAdminReceiver来禁用默认的Android屏幕锁定。

用于在用户解锁屏幕时将Intent.ACTION_SCREEN_ONIntent.ACTION_SCREEN_OFF注册为

时启动您的活动

将这段代码添加到manifestast.xml注册表ScreenReceiver中:

<receiver android:name=".ScreenReceiver">
 <intent-filter>
 <action android:name="android.intent.action.SCREEN_OFF"/>
 <action android:name="android.intent.action.SCREEN_ON"/>
 </intent-filter>
 </receiver>

并将ScreenReceiver.java添加为:

 public class ScreenReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
         if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
         {
            Intent intent = new Intent();  
            intent.setClass(context, ScreenLockActivity.class);
            startActivity(intent);          
         }
    }
}