【问题标题】:How to start default android lock screen from an app other than pressing the power button twice?除了按两次电源按钮之外,如何从应用程序启动默认的 android 锁屏?
【发布时间】:2014-10-03 20:41:10
【问题描述】:

是否有与锁定屏幕相关的意图,以便我可以从我的应用程序启动锁定屏幕?或任何其他方式从应用程序启动锁定屏幕?我尝试使用 goToSleep 和 wakeUp 方法 (http://developer.android.com/reference/android/os/PowerManager.html),以便强制锁定屏幕。但该应用没有 DEVICE_POWER (http://developer.android.com/reference/android/Manifest.permission.html#DEVICE_POWER) 的权限。有什么建议吗?

【问题讨论】:

    标签: android android-intent android-activity sleep lockscreen


    【解决方案1】:

    1) 在您的 onCreate 中添加此意图以提供 Device_Admin 权限

    Intent intent = new Intent(DevicePolicyManager  
                        .ACTION_ADD_DEVICE_ADMIN);  
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,  
                        compName);  
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,  
                        "");  
                startActivityForResult(intent, RESULT_ENABLE);  
    

    2) 在清单中添加接收器类:

     <receiver
                    android:name="com.example.AdminReceiver"
                    android:permission="android.permission.BIND_DEVICE_ADMIN" >
                    <meta-data
                        android:name="android.app.device_admin"
                        android:resource="@layout/policies" >
                        <intent-filter>
                            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" >
                            </action>
                        </intent-filter>
                    </meta-data>
                </receiver>
    

    3) 添加 Receiver 类扩展 DeviceAdminReceiver

        public class AdminReceiver extends DeviceAdminReceiver{  
    
    
            static SharedPreferences getSamplePreferences(Context context) {  
                return context.getSharedPreferences(  
                  DeviceAdminReceiver.class.getName(), 0);  
            }  
     }
    

    4) 在布局文件夹中添加 XML 文件 (policies.xml)

    <!-- ?xml version="1.0" encoding="utf-8"? -->
    <device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <uses-policies>
            <limit-password>
                <watch-login>
                    <reset-password>
                        <force-lock>
                            <wipe-data>
                            </wipe-data>
                        </force-lock>
                    </reset-password>
                </watch-login>
            </limit-password>
        </uses-policies>
    
    </device-admin>
    

    【讨论】:

    • 您是否只想在应用程序启动时锁定手机屏幕?我只是没有正确回答您的问题。
    • 我的应用程序中正在运行一个活动。现在,当我在该活动中按下按钮时,我需要锁定屏幕。就那么简单。但我找不到解决办法。
    • @John 请查看上面编辑的解决方案。找bug,再来
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    相关资源
    最近更新 更多