设备所有者管理员/DevicePolicyManager,可打开移动数据和;Android上的数据自动漫游?

设备所有者管理员/DevicePolicyManager,可打开移动数据和;Android上的数据自动漫游?,android,device-admin,device-owner,android-device-owner,Android,Device Admin,Device Owner,Android Device Owner,因此,我正在开发一个应用程序,在特定的Android设备上作为设备所有者工作。此应用程序在play store上不可用,但可通过NFC从其他设备通过资源调配应用程序进行传输。由于这些设备将非常特定于某些任务(扫描NFC标签),我想从一开始就启用和禁用一些东西 我想禁用声音: devicePolicyManager.setMasterVolumeMuted(adminComponentName, true); 但这似乎根本不起作用,但也不例外 但我真正想做的是启用移动数据和漫游,我们使用的SIM

因此,我正在开发一个应用程序,在特定的Android设备上作为设备所有者工作。此应用程序在play store上不可用,但可通过NFC从其他设备通过资源调配应用程序进行传输。由于这些设备将非常特定于某些任务(扫描NFC标签),我想从一开始就启用和禁用一些东西

我想禁用声音:

devicePolicyManager.setMasterVolumeMuted(adminComponentName, true);
但这似乎根本不起作用,但也不例外

但我真正想做的是启用移动数据和漫游,我们使用的SIM卡支持这一点

devicePolicyManager.setSecureSetting(adminComponentName, Settings.Global.DATA_ROAMING, String.valueOf(1));
devicePolicyManager.setSecureSetting(adminComponentName,"mobile_data",String.valueOf(1));
但遗憾的是,这两行代码引发了一个安全异常:

java.lang.SecurityException: Permission denial: Device owners cannot update mobile_data
有趣的是,插入APN是可行的(在代码后面),有没有机会作为设备管理员/所有者打开移动数据和数据漫游?我是说,这就是做设备管理员的全部目的,对吗

以下是完整的代码供参考:(导致应用程序崩溃的部分被注释掉)


不幸的是,设备所有者无法访问移动数据状态(没错,对设备所有者应用程序的奇怪限制!)

但是,您仍然可以获取移动数据状态,并在状态错误时强制用户将其打开或关闭。以下是代码示例(感谢)

这段代码适用于Android 5-9(尚未在Android 10上测试)

因此,您运行一个后台服务,该服务每隔几秒钟执行一次此检查,并要求用户打开/关闭状态栏中的移动数据


你可以通过克隆这个(这是我的项目)来了解它是如何完成的。方法如下:Utils.isMobileDataEnabled(Context-Context)。

谢谢,我正在使用代码,它工作得非常好。你似乎对设备所有者的应用非常有经验,我仍在想,设备所有者拥有什么样的能力。我仍然有一个问题,那就是设备的键盘不正确(英国而不是德国),因此我正在寻找一种方法,让我的应用程序安装一个新的键盘,或者为三星键盘安装一种新的语言,因为我们使用的是三星设备。我怀疑设备所有者是否能够安装新的键盘或语言。基本上,这里记录了设备所有者可以做什么:据我所知,键盘和语言由/system/build.prop的参数管理:persist.sys.language、persist.sys.region、ro.product.locale.language、ro.product.locale.country。但您必须能够创建一个自定义ROM(或具有root)来更改这些参数。
public static void enableRestrictedAppsAndSettings(Activity activity) {

        ComponentName adminComponentName = DeviceAdminReceiver.getComponentName(activity);
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // disable keyguard and sound
            devicePolicyManager.setKeyguardDisabled(adminComponentName, true);
            devicePolicyManager.setMasterVolumeMuted(adminComponentName, true);

            devicePolicyManager.setSecureSetting(adminComponentName, Settings.Secure.LOCATION_MODE, String.valueOf(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY));

            //devicePolicyManager.setSecureSetting(adminComponentName, Settings.Global.DATA_ROAMING, String.valueOf(1));
            //devicePolicyManager.setSecureSetting(adminComponentName,"mobile_data",String.valueOf(1));

        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (devicePolicyManager.isDeviceOwnerApp(activity.getApplicationContext().getPackageName())) {
                devicePolicyManager.enableSystemApp(adminComponentName,"com.sec.android.app.camera");

                devicePolicyManager.clearUserRestriction(adminComponentName, UserManager.DISALLOW_DATA_ROAMING);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    L.debug("KIOSK", "APN");
                    ApnSetting apn;

                    TelephonyManager manager = (TelephonyManager)activity.getSystemService(Context.TELEPHONY_SERVICE);
                    if (manager.getSimState() == TelephonyManager.SIM_STATE_READY) {

                        String mcc = manager.getSimOperator().substring(0, 3);
                        String mnc = manager.getSimOperator().substring(3);

                        L.debug("KIOSK " + mcc + " "+mnc);

                        apn = new ApnSetting.Builder()
                                .setApnTypeBitmask(ApnSetting.TYPE_DEFAULT)
                                .setApnName("em")
                                .setEntryName("em")
                                .setOperatorNumeric(mcc + mnc) // this is a must its consists from Telephony.Carriers.MCC + Telephony.Carriers.MNC, In my case, I had to pad the MNC with a leading zero
                                .setProtocol(ApnSetting.PROTOCOL_IPV4V6) // this is a must
                                .setRoamingProtocol(ApnSetting.PROTOCOL_IPV4V6) // this is a must
                                .setCarrierEnabled(true)
                                .build();

                        devicePolicyManager.removeOverrideApn(adminComponentName,0);
                        devicePolicyManager.addOverrideApn(adminComponentName, apn);

                        devicePolicyManager.setOverrideApnsEnabled(adminComponentName, true);
                    }
                }

            }
        }
public static boolean isMobileDataEnabled(Context context) {
    ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        Class clazz = Class.forName(cm.getClass().getName());
        Method method = clazz.getDeclaredMethod("getMobileDataEnabled");
        method.setAccessible(true); // Make the method callable
        // get the setting for "mobile data"
        return (Boolean) method.invoke(cm);
    } catch (Exception e) {
        // Let it will be true by default
        return true;
    }
}