Android中以编程方式锁定设备

时间:2012-04-25 09:27:43

标签: android locking device-admin

单击按钮时我必须锁定模拟器。我用这段代码 -

我不明白什么是活动和securemeAdmin

我使用此链接 - > Lock the android device programmatically

public class adminActivity extends DeviceAdminReceiver
{
   public static class Test extends Activity
   {
      Button btn;
      DevicePolicyManager mDPM;
      ComponentName mDeviceAdminSample;

      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

     mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
         mDeviceAdminSample = new ComponentName(Test.this,
                adminActivity.class);

         Intent intent = new   Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);  
         intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, securemeAdmin);

         btn=(Button)findViewById(R.id.btn);

         btn.setOnClickListener(new OnClickListener() 
         {
        @Override
        public void onClick(View v) 
        {
           if (active) 
           {
            mDPM.lockNow();
           }
        }
         });
      }
   }
}

我也使用了键盘管理器,但它也无法使用。有人可以给我一个锁电话的例子吗?

public class Keyguard extends Activity 
{
    Button btn;
    KeyguardLock lock;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn=(Button)findViewById(R.id.btn);

        KeyguardManager mgr = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
        lock = mgr.newKeyguardLock(KEYGUARD_SERVICE);

        btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            lock.reenableKeyguard();
        }
    });
    }
}

2 个答案:

答案 0 :(得分:3)

此代码适用于您..

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 0;
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 

答案 1 :(得分:1)

要锁定Android设备,您必须需要管理员权限。您必须处理三件事1)DeviceAdminReceiver类2)清单文件3)您调用DevicePolicyManager的锁定方法的Activity类。请参阅此链接http://developer.android.com/guide/topics/admin/device-admin.html