简体   繁体   English

Android for work - 如何检查我的应用程序是否在工作配置文件中运行?

[英]Android for work - How to check if my application is running in the work profile?

I'm creating an app that needs to behave differently if it's running in the work profile.我正在创建一个应用程序,如果它在工作配置文件中运行,则该应用程序的行为需要有所不同。

There is any possibility to know that?有没有可能知道?

The documentation has nothing about it and I already tried to add a restriction that is only available in the work profile and it works, but I need a solution without any action from the administrator.该文档对此一无所知,我已经尝试添加仅在工作配置文件中可用的限制并且它有效,但我需要一个无需管理员采取任何操作的解决方案。

Android for work information: http://www.android.com/work/ Android 工作信息: http : //www.android.com/work/

Android for work documentation: https://developer.android.com/training/enterprise/index.html Android 工作文档: https : //developer.android.com/training/enterprise/index.html

I found a solution : if isProfileOwnerApp return true for one package name, it means that your app (badged) is running on work profile.我找到了一个解决方案:如果 isProfileOwnerApp 对一个包名称返回 true,则意味着您的应用程序(标记)正在工作配置文件上运行。 if your app is running in normal mode (no badge) isProfileOwnerApp return false for all admins even if there is a Work profile.如果您的应用程序在正常模式下运行(无徽章) isProfileOwnerApp 为所有管理员返回 false,即使有工作配置文件。

DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
List<ComponentName> activeAdmins =   devicePolicyManager.getActiveAdmins();
if (activeAdmins != null){
   for (ComponentName admin : activeAdmins){
      String packageName=  admin.getPackageName();
      Log.d(TAG, "admin:"+packageName);
      Log.d(TAG, "profile:"+ devicePolicyManager.isProfileOwnerApp(packageName));
      Log.d(TAG, "device:"+ devicePolicyManager.isDeviceOwnerApp(packageName));
   }
}

I took the response from @earlypearl into a Kotlin class:我把@earlypearl 的回复放到了 Kotlin 类中:

class UserProfile(context: Context) {
    private val weakContext = WeakReference(context)

    val isInstalledOnWorkProfile: Boolean
        get() {
            return weakContext.get()?.let {
                val devicePolicyManager =
                    it.getSystemService(AppCompatActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager
                val activeAdmins = devicePolicyManager.activeAdmins

                activeAdmins?.any { devicePolicyManager.isProfileOwnerApp(it.packageName) } ?: false
            } ?: false
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式检查我的 android 设备是否已激活工作资料? - Programatically Check whether my android device has activated work profile or not? 如何检查Bugsense是否在我的Android应用程序中运行 - How to check if Bugsense is running in my Android application 如何检查我的应用程序是否在android(不是作为服务)中运行? - How to check if my application is running in android(not as a service)? 通过 ADB 检查 Android 上的工作资料是否处于活动状态? - Check if a work profile is active on Android via ADB? 以编程方式检查 android 中的“工作资料”支持 - Programmatically check 'Work Profile' support in android 如果我的应用程序没有运行,AlarmManager会工作吗? - Will AlarmManager work if my application is not running? 如何区分在托管配置文件和常规配置文件下运行的Android应用 - How to distinguish android application running under managed profile and regular profile? android:检查服务是否正在运行不起作用 - android: check if a service is running don't work 如何在 Android Enterprise Work Profile 中调试应用程序 - How to debug Apps in Android Enterprise Work Profile Android:如何检查应用程序是否正在运行 - Android: How to check application is Running or not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM