zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

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

Android接口应用 实现 如何 原生 使用
2023-09-27 14:27:46 时间

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

前端时间做项目的时候,突然发现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
  1. /** 
  2.  * Initiates the managed profile provisioning. If we already have a managed profile set up on 
  3.  * this device, we will get an error dialog in the following provisioning phase. 
  4.  */  
  5. private void provisionManagedProfile() {  
  6.     Activity activity = getActivity();  
  7.     if (null == activity) {  
  8.         return;  
  9.     }  
  10.     Intent intent = new Intent(ACTION_PROVISION_MANAGED_PROFILE);  
  11.     intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,  
  12.                     activity.getApplicationContext().getPackageName());  
  13.     if (intent.resolveActivity(activity.getPackageManager()) != null) {  
  14.         startActivityForResult(intent, REQUEST_PROVISION_MANAGED_PROFILE);  
  15.         activity.finish();  
  16.     } else {  
  17.         Toast.makeText(activity, "Device provisioning is not enabled. Stopping.",  
  18.                 Toast.LENGTH_SHORT).show();  
  19.     }  
  20. }  

2:创建成功后
   通过之前传入的DeviceAdminReceiver,可以接收到消息,这边可以提醒用户
3:启动企业空间配置应用
   企业空间创建成功之后,会在桌面生成企业空间的组,系统会默认将一些应用加入企业空间
启动这边的BoboUtils就可以对需要双开的应用进行配置。

主要调用以下方法开启、关闭需要双开的应用
  
  
[java] view plain copy
  1. /** 
  2.      * Enables or disables the specified app in this profile. 
  3.      * 
  4.      * @param packageName The package name of the target app. 
  5.      * @param enabled     Pass true to enable the app. 
  6.      */  
  7.     private void setAppEnabled(String packageName, boolean enabled) {  
  8.         Activity activity = getActivity();  
  9.         if (null == activity) {  
  10.             return;  
  11.         }  
  12.         PackageManager packageManager = activity.getPackageManager();  
  13.         DevicePolicyManager devicePolicyManager =  
  14.                 (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);  
  15.         try {  
  16.             ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName,  
  17.                     PackageManager.GET_UNINSTALLED_PACKAGES);  
  18.             // Here, we check the ApplicationInfo of the target app, and see if the flags have  
  19.             // ApplicationInfo.FLAG_INSTALLED turned on using bitwise operation.  
  20.             if (0 == (applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED)) {  
  21.                 // If the app is not installed in this profile, we can enable it by  
  22.                 // DPM.enableSystemApp  
  23.                 if (enabled) {  
  24.                     devicePolicyManager.enableSystemApp(  
  25.                             BasicDeviceAdminReceiver.getComponentName(activity), packageName);  
  26.                 } else {  
  27.                     // But we cannot disable the app since it is already disabled  
  28.                     Log.e(TAG, "Cannot disable this app: " + packageName);  
  29.                     return;  
  30.                 }  
  31.             } else {  
  32.                 // If the app is already installed, we can enable or disable it by  
  33.                 // DPM.setApplicationHidden  
  34.                 devicePolicyManager.setApplicationHidden(  
  35.                         BasicDeviceAdminReceiver.getComponentName(activity), packageName, !enabled);  
  36.             }  
  37.             Toast.makeText(activity, enabled ? R.string.enabled : R.string.disabled,  
  38.                     Toast.LENGTH_SHORT).show();  
  39.         } catch (PackageManager.NameNotFoundException e) {  
  40.             Log.e(TAG, "The app cannot be found: " + packageName, e);  
  41.         }  
  42.     }  

  
  
[java] view plain copy
  1.   
源代码可以从 https://github.com/bobohuang1985/android-utils-api 下载,具体代码位置在
utils.bobo.com.boboutils.MultiApp包内,