何我以编程方式锁定我的Android手机? 我尝试了this示例。但是当我点击启用按钮时,Activity会弹出几毫秒然后自动关闭
日志显示此日志没有错误
Log.i("DeviceAdminSample", "Admin enable FAILED!");
任何人都可以告诉我如何锁定安卓屏幕(就像锁定时多次尝试模式锁定和手机锁定一样)
感谢任何帮助
答案 0 :(得分:21)
你必须让你的应用成为管理员,阅读这里的东西
创建一个新的空项目,并创建一个名为MyAdminReceiver
的类,像这样扩展DeviceAdminReceiver
import android.app.admin.DeviceAdminReceiver;
public class MyAdminReceiver extends DeviceAdminReceiver{
}
创建一个名为xml的新文件夹,并为名为admin.xml
的管理员权限创建一个.xml文件并添加策略,在这种情况下,它会锁定屏幕
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>
在清单中,在“应用程序标记”
下添加接收者<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
添加此代码
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,则可能会出现使用Intent.FLAG_ACTIVITY_NEW_TASK
的错误,但是当您使用该窗口时可能不会像你的情况那样弹出,所以尝试只从活动的子类中打开它
除非尚未注册为管理员,否则您无法卸载应用
答案 1 :(得分:0)
但是当我点击启用按钮时,Activity会弹出几毫秒然后自动关闭
该示例中显示的代码将在单击时显示“设置”应用程序。
任何人都可以告诉我如何锁定Android屏幕
您使用链接的代码。 Here is my sample app显示相同的基本内容。
具体做法是:
您需要在清单中设置BroadcastReceiver
设置为设备管理组件
用户必须以设备管理员身份激活您的应用
然后,您在lockNow()
DevicePolicyManager