我正在从onServiceConnected()
的{{1}}方法开始一项活动。
我成功启动了AccessibilityService
,但无法从中启动设备管理意图。
我调试了我的应用程序,服务已成功启动,并且AccessibilityService
也称为onServiceConnected()
的执行也没有崩溃,但DeviceAdmin意图仍未启动。
callDeviceAdmin()
更新
public class AccessibilityEventService extends AccessibilityService {
public static final int REQUEST_CODE_FOR_ACTIVE_DEVICE_ADMIN = 100;
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
@Override
public void onInterrupt() {
Log.d("TAG", "EXECUTE ON Interrupt");
}
@Override
protected void onServiceConnected() {
callDeviceAdmin();
}
private void callDeviceAdmin(){
ComponentName mComponentName;
DevicePolicyManager mDevicePolicyManager;
mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
mComponentName = new ComponentName(this, AdminReceiver.class);
try{
boolean isAdmin = mDevicePolicyManager.isAdminActive(mComponentName);
if (!isAdmin) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "SaferCell wants device Administrator access");
getApplicationContext().startActivity(intent);
}
} catch (Exception e) {
Log.d("onAccessibilityEvent", e.getMessage());
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
您也可以尝试使用代码,但是我创建了一个演示可访问性服务来尝试此操作,并且运行良好。
您必须添加Intent标志,因为要从Activity上下文外部启动活动需要FLAG_ACTIVITY_NEW_TASK
标志。
public class AccessibilityServiceDemo extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
}
@Override
public void onInterrupt() {
}
@Override
protected void onServiceConnected() {
super.onServiceConnected();
System.out.println("onServiceConnected");
Intent intent = new Intent().setComponent(new ComponentName("com.android.settings", "com.android.settings.DeviceAdminSettings"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}