问题描述
浩做编程方式锁住我的Android手机?我想下面的这个的例子。但是,当我点击启用按钮活动弹出的几毫秒后自动关闭
Ho do i lock my android phone programmatically ?I tried following this example. But when i click on the enable button the Activity pops up for few milliseconds and then closes automatically
日志显示没有错误只是这个日志
The log shows no error just this log
Log.i("DeviceAdminSample", "Admin enable FAILED!");
任何人能告诉我如何锁定屏幕的Android(如锁时做出许多尝试的模式锁,手机锁下)
Can any one tell me how to lock the android screen (Like the lock when make to many attempts in pattern lock and the phone locks down)
任何帮助是pciated AP $ P $
Any help is appreciated
推荐答案
您必须使您的应用程序作为管理员,了解这里有什么事
You have to make your app as admin, Read something over here
创建一个新的空项目,并创建了一个名为 MyAdminReceiver
扩展 DeviceAdminReceiver
像这样
Create a new empty project and create a class called MyAdminReceiver
that extends DeviceAdminReceiver
like this
import android.app.admin.DeviceAdminReceiver;
public class MyAdminReceiver extends DeviceAdminReceiver{
}
创建一个名为XML的新的文件夹,并创建一个.xml文件名为您的管理员权限 admin.xml的
并添加策略,在你情况下,它锁定屏幕
Create a new folder called xml and create an .xml file for your admin rights called admin.xml
and add the policies, in you case its locking the screen
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>
在您的清单下的应用程序添加标签的接收器
In your manifest add the receiver under Application tag
<receiver
android:name="MyAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
而在你的 MainActivity.java
添加code这样
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private static final int ADMIN_INTENT = 15;
private static final String description = "Some Description About Your Admin";
private DevicePolicyManager mDevicePolicyManager;
private ComponentName mComponentName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDevicePolicyManager = (DevicePolicyManager)getSystemService(
Context.DEVICE_POLICY_SERVICE);
mComponentName = new ComponentName(this, MyAdminReceiver.class);
Button btnEnableAdmin = (Button) findViewById(R.id.btnEnableAdmin);
Button btnDisableAdmin = (Button) findViewById(R.id.btnDisableAdmin);
Button btnLock = (Button) findViewById(R.id.btnLock);
btnEnableAdmin.setOnClickListener(this);
btnDisableAdmin.setOnClickListener(this);
btnLock.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnEnableAdmin:
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);
break;
case R.id.btnDisableAdmin:
mDevicePolicyManager.removeActiveAdmin(mComponentName);
Toast.makeText(getApplicationContext(), "Admin registration removed", Toast.LENGTH_SHORT).show();
break;
case R.id.btnLock:
boolean isAdmin = mDevicePolicyManager.isAdminActive(mComponentName);
if (isAdmin) {
mDevicePolicyManager.lockNow();
}else{
Toast.makeText(getApplicationContext(), "Not Registered as admin", Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADMIN_INTENT) {
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "Registered As Admin", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Failed to register as Admin", Toast.LENGTH_SHORT).show();
}
}
}
}
注意:如果您尝试调用意向管理设备等,从一个活动的子类有机会,你可能会使用一个错误 Intent.FLAG_ACTIVITY_NEW_TASK
但是当你用你的窗口可能不会弹出像你的情况,以便尝试从活动的子类,打开它只是
Note: If you try to call the Intent for Admin Device other that from an Activity subclass there are chances you might get an error to use Intent.FLAG_ACTIVITY_NEW_TASK
but when you use that your window might not pop like in your case so Try opening it from a subclass of an activity only
你也不能卸载你的应用程序,除非它没有被注销,因为管理
Also you cannot un-install your app unless it has not be unregistered as an admin
这篇关于我如何锁定手机编程机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!