我希望我的应用程序在单击按钮时锁定手机!以下是我的代码。
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;
}
}
}
这是行不通的。我还尝试在 onClick 事件中使用以下代码。
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.release();
这也行不通。谁能帮我 ?