繁体   English   中英

除了两次按电源按钮外,如何从应用程序启动默认的Android锁定屏幕?

[英]How to start default android lock screen from an app other than pressing the power button twice?

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

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM