简体   繁体   English

如何在按键单击事件上锁定手机? -安卓

[英]How to lock phone on a button click event? - android

I want my application to lock the phone when a button is clicked ! 我希望我的应用程序在单击按钮时锁定手机! Following is my code. 以下是我的代码。

public class MainActivity extends Activity implements OnClickListener {

    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = (Button) findViewById(R.id.button);

        b.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
        case R.id.button:
        KeyguardManager km = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
            KeyguardLock kl = km.newKeyguardLock(KEYGUARD_SERVICE);
            kl.reenableKeyguard();


            break;
        }

    }

}

This is not working. 这是行不通的。 I also tried with the following code in onClick event. 我还在onClick事件中尝试了以下代码。

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
         wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
        wl.release();

This is also not working. 这也不起作用。 Can anyone help me ? 谁能帮我 ?

You need to set up a device admin component , then call lockNow() on the DevicePolicyManager . 您需要设置设备管理组件 ,然后在DevicePolicyManager上调用lockNow()

This sample project shows what is required from a coding standpoint. 该示例项目从编码的角度显示了所需的内容。 Once installed, the user must agree to make your app be a device administrator. 安装后,用户必须同意让您的应用成为设备管理员。 The activity will route the user to the proper screen in the Settings app for this if the app is not a device administrator: 如果该应用不是设备管理员,则该活动会将用户定向到“设置”应用中的相应屏幕:

public class LockMeNowActivity extends Activity {
  private DevicePolicyManager mgr=null;
  private ComponentName cn=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    cn=new ComponentName(this, AdminReceiver.class);
    mgr=(DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
  }

  public void lockMeNow(View v) {
    if (mgr.isAdminActive(cn)) {
      mgr.lockNow();
    }
    else {
      Intent intent=
          new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
      intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
      intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                      getString(R.string.device_admin_explanation));
      startActivity(intent);
    }
  }
}

Use this code in your onCreate() method of activity to initialize DevicePolicyManager : 在活动的onCreate()方法中使用以下代码初始化DevicePolicyManager:

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

To lock the device write the code in the event where you use to lock set a boolean enable : 要锁定设备,请在用于锁定的事件中编写代码,并设置一个布尔启用:

if (enable) {
myDevicePolicyManager.lockNow();
}

May be you have to enable the device admin, the DevicePolicyManager intent is called and it should be enabled by the user. 可能是您必须启用设备管理员,调用了DevicePolicyManager意图,并且应该由用户启用它。 Follow this code : 请遵循以下代码:

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

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

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