如何在设置Android中禁用应用程序的卸载按钮

时间:2019-03-29 09:48:28

标签: android device-admin

我正在实施父母与孩子的应用程序

  1. 父母可以追踪他们的孩子
  2. 孩子无法卸载应用。

我正在使用设备管理功能来实现此功能。

启用管理员后,我想禁用儿童应用程序中的卸载按钮

image

这是我的代码。

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private PolicyManager policyManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    policyManager = new PolicyManager(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
        case R.id.activate_admin:
            if (!policyManager.isAdminActive()) {
                Intent activateDeviceAdmin = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                activateDeviceAdmin.putExtra(DevicePolicyManager.ACTION_SET_NEW_PASSWORD,"abcdefgh");
                activateDeviceAdmin.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, policyManager.getAdminComponent());
                activateDeviceAdmin.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "After activating admin, you will be able to block application uninstallation.");

                startActivityForResult(activateDeviceAdmin,
                        PolicyManager.DPM_ACTIVATION_REQUEST_CODE);

            }
            break;
        case R.id.deactivate_admin:
            if (policyManager.isAdminActive())
                policyManager.disableAdmin();
            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (resultCode == this.RESULT_OK && requestCode == PolicyManager.DPM_ACTIVATION_REQUEST_CODE) {
        // handle code for successfull enable of admin
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
}

MyReceiver.java

public class SampleDeviceAdminReceiver extends DeviceAdminReceiver {

@Override
public void onDisabled(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "disabled dpm", Toast.LENGTH_SHORT).show();
    super.onDisabled(context, intent);
}

@Override
public void onEnabled(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "enabled dpm", Toast.LENGTH_SHORT).show();
    super.onEnabled(context, intent);
}

@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "disable dpm request", Toast.LENGTH_SHORT).show();
    return super.onDisableRequested(context, intent);
}



}

PolicyManager.java

public class PolicyManager {

public static final int DPM_ACTIVATION_REQUEST_CODE = 100;

private Context mContext;
private DevicePolicyManager mDPM;
private ComponentName adminComponent;

public PolicyManager(Context context) {
    // TODO Auto-generated constructor stub
    this.mContext = context;
    mDPM = (DevicePolicyManager) mContext
            .getSystemService(Context.DEVICE_POLICY_SERVICE);
    adminComponent = new ComponentName(mContext.getPackageName(),
            mContext.getPackageName() + ".SampleDeviceAdminReceiver");
}

public boolean isAdminActive() {
    return mDPM.isAdminActive(adminComponent);
}

public ComponentName getAdminComponent() {
    return adminComponent;
}

public void disableAdmin() {
    mDPM.removeActiveAdmin(adminComponent);
}
}

0 个答案:

没有答案