如何使用Android原生接口,实现“应用双开”
2017-05-17 17:50
821 查看
第一次使用应用双开功能的时候,感觉好神奇,一直想研究他是怎么实现的,无奈反编译后看不懂,就此作罢。前端时间做项目的时候,突然发现android 5.0以后引入的一个神奇的功能--Android in the Enterprise,我称之为--企业空间。Android 5.0以后允许在原本用户上面,创建一个企业空间,在企业空间里,可以包含有多个应用,这些应用和原本用户的应用是独立的。2个空间(企业空间和用户空间)之前的数据也是分开的。这样可以更好的保证企业空间内应用数据的保密性,在企业空间内甚至可以设置一个远程的管理员,管理员可以设置企业空间内的各个应用的 权限,比如说能否访问某个网址之类的,还有非常多的高级功能,有兴趣的同学可以阅读google的官方文档。 https://developer.android.com/work/overview.html 由于企业空间和原本用户空间是独立的,而且可以独立同时运行,所以我们可以用这个来实现一个“”应用双开“。我参考google官方demo做了一个类似应用双开的功能。
这边大概介绍下流程:
1:创建一个企业空间
/** * 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就可以对需要双开的应用进行配置。
主要调用以下方法开启、关闭需要双开的应用
/** * 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); } }
源代码可以从 https://github.com/bobohuang1985/android-utils-api 下载,具体代码位置在
utils.bobo.com.boboutils.MultiApp包内,欢迎大家提出意见。大家可以通过QQ群,或者微信公众号交流:
相关文章推荐
- 如何使用Android原生接口,实现“应用双开”
- 关于android webview 端调原生app 的支付宝接口实现 还有BeeCloud(秒支付) 的接入使用
- 如何实现监听Android输入法弹出收回,为什么要使用接口
- 如何使用Android实现接口实信息在留言板显示
- Android应用中使用及实现系统“分享”接口 推荐
- Android应用中使用及实现系统“分享”接口
- Android应用中使用及实现系统“分享”接口
- Android应用中使用及实现系统“分享”接口实例
- Android应用中使用及实现系统“分享”接口
- Android应用中使用及实现系统“分享”接口
- android如何使用BroadcastReceiver后台实现来电通话记录的监听并存取到sqllite数据库通过Contentprovilder实现接口
- Android应用中使用及实现系统“分享”接口
- Android应用中使用及实现系统“分享”接口
- Android应用中使用及实现系统“分享”接口
- H5 Web App 的性能测试平台 : 使用 Frida 实现 AOP 拦截 hook Android 原生应用的方法
- Android应用中使用及实现系统“分享”接口
- Android应用如何实现换肤功能
- Android应用开发中如何使用隐藏API
- 如何使用Android SDK开发Android应用?
- 如何使用android SDK开发android 应用