我尝试以设备管理员身份启用我的应用程序。尝试调用活动的代码如下所示,该活动使我的应用程序成为设备管理员,这是我的应用程序的第一个活动:
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.common.GooglePlayServicesUtil;
public class RegisterActivity extends Activity {
// alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Internet detector
ConnectionDetector cd;
// UI elements
EditText txtName;
EditText txtEmail;
// Register button
Button btnRegister;
static final int ACTIVATION_REQUEST = 47; //Request ID for Device Administrator
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//Unrelated
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status != 0) {
Toast.makeText(this, "This device is not supported - please download Google Play Services.",
Toast.LENGTH_LONG).show();
}
//Begin enabling device administrator
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
ComponentName deviceAdminComponentName = new ComponentName(this, DeviceAdmin.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminComponentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "You must enable device administration for certain features"
+ " of the app to function.");
//I thought that this would start the activity that lets the user
//choose whether to enable the app as a device admin
startActivityForResult(intent, ACTIVATION_REQUEST);
//Also contains code pertaining to unrelated Google Cloud messaging features
//and an onClick() method, all inside of onCreate()
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTIVATION_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Log.i("DeviceAdminSample", "Administration enabled!");
}
else {
Log.i("DeviceAdminSample", "Administration enable FAILED!");
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
使用此代码,当我运行我的应用程序时,活动出现,然后有动画,它看起来正在打开一个新活动,但随后该动画停止并保持当前活动而不提示用户启用设备管理员。我的所有关于设备管理员的日志消息都没有被点击。但是,在Settings-> Security->设备管理员页面上查看当前的设备管理员时,我的应用程序会显示,但未作为设备管理员进行检查。
以下是我的DeviceAdmin子类的代码,虽然我无法在此代码中看到问题的来源:
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class DeviceAdmin extends DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
Log.i("Device Admin: ", "Enabled");
}
@Override
public String onDisableRequested(Context context, Intent intent) {
return "Admin disable requested";
}
@Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
Log.i("Device Admin: ", "Disabled");
}
@Override
public void onPasswordChanged(Context context, Intent intent) {
super.onPasswordChanged(context, intent);
Log.i("Device Admin: ", "Password changed");
}
}
非常感谢任何帮助!
更新:Android Manifest中的Device Admin接收器如下:
<!-- Device Administration Receiver -->
<receiver
android:name="com.myPackage.DeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
答案 0 :(得分:5)
我猜你忘了用正确的意图过滤器在清单中添加接收器
<receiver
android:name="com.yourpackage.DeviceAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<intent-filter>
This action is required
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
This is required this receiver to become device admin component.
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
</receiver>
和XMl
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
<expire-password />
<encrypted-storage />
<disable-camera />
</uses-policies>
</device-admin>
有关设备管理的详细信息。 Check this
答案 1 :(得分:2)
我在Jagadesh的帮助下找到了问题。
在我的Android Manifest文件中,我将接收者名称设置为 DeviceAdminReceiver ,如此处所示
<receiver
android:name="com.myPackage.DeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
我错了,接收器应该带有类 DeviceAdminReceiver 的名称,而不是我编写的扩展 DeviceAdminReceiver , DeviceAdmin的子类的名称
清单文件中更新的工作接收器是:
<!-- Device Administration Receiver -->
<receiver
android:name="com.myPackage.DeviceAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
答案 2 :(得分:0)
我遇到了同样的问题,并通过删除清单来纠正了它:
android:launchMode="singleInstance"