Android屏幕以特定的不同时间间隔(例如1分钟,2分钟)以编程方式锁定

时间:2014-05-27 09:38:06

标签: android locking

我开发此类应用程序,我想通过用户通过对话框选择的特定时间自动锁定屏幕。

请帮帮我,我不知道。 当用户点击时间间隔的对话文本时,对话框关闭,屏幕在特定时间自动选择,用户选择,不活动后。

2 个答案:

答案 0 :(得分:0)

在您的活动中定义:

private DevicePolicyManager mDevicePolicyManager;
private ComponentName mComponentName;

    mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    mComponentName = new ComponentName(this, MyAdminReceiver.class); 

注册并取消注册点击主要活动:

txtRegister.setOnClickListener(new OnClickListener() {
                //
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(
                            DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                            mComponentName);
                    intent.putExtra(
                            DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                            description);
                    startActivityForResult(intent, ADMIN_INTENT);

                }
            });
            txtUnRegister.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {


                    mComponentName = new ComponentName(MainActivity.this, MyAdminReceiver.class);
                    mDevicePolicyManager.removeActiveAdmin(mComponentName);
                    Toast.makeText(getApplicationContext(),
                            "Admin registration removed",
                            Toast.LENGTH_SHORT).show();

                }
            });     

创建管理类MyAdminReceiver:

import android.app.admin.DeviceAdminReceiver;

public class MyAdminReceiver extends DeviceAdminReceiver{

}      

添加你的menifist:

<receiver
        android:name="MyAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@layout/admin" />

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
</receiver>  

创建一个名为admin.xml的xml文件:

<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-policies>
    <force-lock />
</uses-policies>

在您的活动中写下您想要执行操作的代码(也可以点击它):

boolean isAdmin = mDevicePolicyManager.isAdminActive(mComponentName);
    if (isAdmin) {
        Handler mHandler = new Handler();

        mHandler.post(new Runnable() {

            @Override
            public void run() {

                final Handler handler = new Handler();
                Timer t = new Timer();
                t.schedule(new TimerTask() {
                    public void run() {
                        handler.post(new Runnable() {
                            public void run() {
                                mDevicePolicyManager.lockNow();
                            }
                        });
                    }
                }, delay); // flashing of screen that you want to time delay
            }
        });

    } else {
        Toast.makeText(getApplicationContext(), "first register as admin",
                Toast.LENGTH_SHORT).show();
    }                                                                      

答案 1 :(得分:0)

我为您提供另一种屏蔽特定时间的最佳方式,如下所示:

在您的活动中声明:

 int defTimeOut = 0;

在您的特定时间间隔内的活动中编写以下代码,请点击:

    final int DELAY = 60000; // 1 minute you change here as your time interval click
    defTimeOut = Settings.System.getInt(getContentResolver(),
            Settings.System.SCREEN_OFF_TIMEOUT, DELAY);
    Settings.System.putInt(getContentResolver(),
            Settings.System.SCREEN_OFF_TIMEOUT, DELAY);

在您的活动中销毁:

@Override
protected void onDestroy() 
{
    super.onDestroy();
    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, defTimeOut);
}

在您的清单文件中获得以下权限:

   <uses-permission android:name="android.permission.WRITE_SETTINGS" />