您的位置:首页 > 移动开发 > Android开发

如何使用Android原生接口,实现“应用双开”

2017-11-08 20:30 731 查看
第一次使用应用双开功能的时候,感觉好神奇,一直想研究他是怎么实现的,无奈反编译后看不懂,就此作罢。

前端时间做项目的时候,突然发现android 5.0以后引入的一个神奇的功能--Android in the Enterprise,我称之为--企业空间。

Android 5.0以后允许在原本用户上面,创建一个企业空间,在企业空间里,可以包含有多个应用,这些应用和原本用户的应用是独立的。2个空间(企业空间和用户空间)之前的数据也是分开的。这样可以更好的保证企业空间内应用数据的保密性,在企业空间内甚至可以设置一个远程的管理员,管理员可以设置企业空间内的各个应用的 权限,比如说能否访问某个网址之类的,还有非常多的高级功能,有兴趣的同学可以阅读google的官方文档。
https://developer.android.com/work/overview.html
由于企业空间和原本用户空间是独立的,而且可以独立同时运行,所以我们可以用这个来实现一个“”应用双开“。

我参考google官方demo做了一个类似应用双开的功能。

这边大概介绍下流程:
1:创建一个企业空间
[java] view plain copy /** 
 * Initiates the managed profile provisioning. If we already have a managed profile set up on 
 * this device, we will get an error dialog in the following provisioning phase. 
 */  
private void provisionManagedProfile() {  
    Activity activity = getActivity();  
    if (null == activity) {  
        return;  
    }  
    Intent intent = new Intent(ACTION_PROVISION_MANAGED_PROFILE);  
    intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,  
                    activity.getApplicationContext().getPackageName());  
    if (intent.resolveActivity(activity.getPackageManager()) != null) {  
        startActivityForResult(intent, REQUEST_PROVISION_MANAGED_PROFILE);  
        activity.finish();  
    } else {  
        Toast.makeText(activity, "Device provisioning is not enabled. Stopping.",  
                Toast.LENGTH_SHORT).show();  
    }  
}  

2:创建成功后
通过之前传入的DeviceAdminReceiver,可以接收到消息,这边可以提醒用户
3:启动企业空间配置应用
企业空间创建成功之后,会在桌面生成企业空间的组,系统会默认将一些应用加入企业空间
启动这边的BoboUtils就可以对需要双开的应用进行配置。
主要调用以下方法开启、关闭需要双开的应用
[java] view plain copy /** 
     * Enables or disables the specified app in this profile. 
     * 
     * @param packageName The package name of the target app. 
     * @param enabled     Pass true to enable the app. 
     */  
    private void setAppEnabled(String packageName, boolean enabled) {  
        Activity activity = getActivity();  
        if (null == activity) {  
            return;  
        }  
        PackageManager packageManager = activity.getPackageManager();  
        DevicePolicyManager devicePolicyManager =  
                (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);  
        try {  
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName,  
                    PackageManager.GET_UNINSTALLED_PACKAGES);  
            // Here, we check the ApplicationInfo of the target app, and see if the flags have  
            // ApplicationInfo.FLAG_INSTALLED turned on using bitwise operation.  
            if (0 == (applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED)) {  
                // If the app is not installed in this profile, we can enable it by  
                // DPM.enableSystemApp  
                if (enabled) {  
                    devicePolicyManager.enableSystemApp(  
                            BasicDeviceAdminReceiver.getComponentName(activity), packageName);  
                } else {  
                    // But we cannot disable the app since it is already disabled  
                    Log.e(TAG, "Cannot disable this app: " + packageName);  
                    return;  
                }  
            } else {  
                // If the app is already installed, we can enable or disable it by  
                // DPM.setApplicationHidden  
                devicePolicyManager.setApplicationHidden(  
                        BasicDeviceAdminReceiver.getComponentName(activity), packageName, !enabled);  
            }  
            Toast.makeText(activity, enabled ? R.string.enabled : R.string.disabled,  
                    Toast.LENGTH_SHORT).show();  
        } catch (PackageManager.NameNotFoundException e) {  
            Log.e(TAG, "The app cannot be found: " + packageName, e);  
        }  
    }  



[java] view plain copy   

源代码可以从 https://github.com/bobohuang1985/android-utils-api 下载,具体代码位置在
utils.bobo.com.boboutils.MultiApp包内,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: