gpt4 book ai didi

android - 锁定安卓手机

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:50:27 25 4
gpt4 key购买 nike

我试图在用户按下按钮时以编程方式锁定设备。我知道我需要使用 deviceAdminReciever 并且我已经这样做了但是我的应用程序在我运行时崩溃

以下是我的 list :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyApp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver
android:name=".MainActivity"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_sample" />

<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>

<uses-feature android:name="android.hardware.camera" />

</manifest>

以下是我的java代码:

public class MainActivity extends DeviceAdminReceiver {

public static class MyActivity extends Activity {

protected static final int ACTIVATION_REQUEST = 1;
private ImageButton btn;

private DevicePolicyManager mDPM;
private ComponentName mDeviceAdminSample;

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

mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
mDeviceAdminSample = new ComponentName(Potter.this,
MainActivity.class);
setContentView(R.layout.activity_main);



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

btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Intent intent = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
mDeviceAdminSample = new ComponentName(this, MainActivity.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
mDeviceAdminSample);
startActivityForResult(intent, ACTIVATION_REQUEST);
mDPM.lockNow();

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case ACTIVATION_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Log.i("DeviceAdminSample", "Administration enabled!");
} else {
Log.i("DeviceAdminSample", "Administration enable FAILED!");
}
return;


}
}


}
}

但是,当我运行应用程序时它崩溃了。我该如何纠正这个问题?

最佳答案

这是我的有效代码:

list :

<application
android:icon="@drawable/ic_power_settings_new_black_48dp"
android:label="Screen Off"
android:theme="@android:style/Theme.NoDisplay">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="android.app.admin.DeviceAdminReceiver">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>

主要 Activity .java:

package com.example.anhkhoachu.screenoff;

import android.app.Activity;
import android.app.Application;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;


public class MainActivity extends Activity {
DevicePolicyManager dpm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
try {
dpm.lockNow();
finish();
} catch (SecurityException e) {
Intent it = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
it.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, new ComponentName(this, DeviceAdminReceiver.class));
startActivityForResult(it, 0);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
dpm.lockNow();
} else {
Toast.makeText(this, "Device administrator must be activated to lock the screen", Toast.LENGTH_LONG).show();
}
finish();
}}

device_admin.xml:

<device-admin>
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>

关于android - 锁定安卓手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13672404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com