为什么startactivity()从接收器激活mdm提示符时会闪烁提示符而不打开它?

ssgvzors  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(283)

在一个小型的mdm(设备管理器)应用程序中,我们试图实现以下功能:安装应用程序后立即弹出“激活设备管理器”对话框。此应用程序必须在企业环境中使用adb安装在许多设备上,如果能够实现此功能,将大大简化安装过程。使用此代码(我们的 DeviceAdminReceiver 都叫同一个名字, DeviceAdminReceiver )要弹出提示:

  1. public class PackageReceiver extends BroadcastReceiver {
  2. private static final String PACKAGE_STRING = "package:";
  3. private static final String REPLACEMENT_STRING = "";
  4. @Override
  5. public void onReceive(Context context, Intent intent) {
  6. try{
  7. boolean isSelf = intent.getDataString()
  8. .replace(PACKAGE_STRING,REPLACEMENT_STRING)
  9. .equals(BuildConfig.APPLICATION_ID);
  10. switch (intent.getAction()){
  11. case Intent.ACTION_PACKAGE_ADDED : case Intent.ACTION_PACKAGE_REPLACED :
  12. if (isSelf){
  13. Intent activateMDM =
  14. new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  15. activateMDM.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  16. activateMDM.putExtra(
  17. DevicePolicyManager.EXTRA_DEVICE_ADMIN,
  18. DeviceAdminReceiver.getComponent(context)
  19. );
  20. context.startActivity(activateMDM);
  21. }
  22. break;
  23. }
  24. }catch (Exception e){
  25. e.printStackTrace();
  26. }
  27. }

在舱单中有此声明时:

  1. <receiver android:name=".receivers.PackageReceiver" android:enabled="true" android:exported="true">
  2. <intent-filter>
  3. <category android:name="android.intent.category.DEFAULT" />
  4. <action android:name="android.intent.action.PACKAGE_ADDED" />
  5. <action android:name="android.intent.action.PACKAGE_CHANGED" />
  6. <action android:name="android.intent.action.PACKAGE_INSTALL" />
  7. <action android:name="android.intent.action.PACKAGE_REPLACED" />
  8. <action android:name="android.intent.action.PACKAGE_RESTARTED" />
  9. <action android:name="android.intent.action.PACKAGES_UNSUSPENDED" />
  10. <action android:name="android.intent.action.PACKAGES_SUSPENDED" />
  11. <data android:scheme="package" />
  12. </intent-filter>
  13. </receiver>

问题是,此代码导致安装完成时提示闪烁,但它没有保持足够长的打开时间,用户甚至无法点击“激活”。
根据文档,我们添加了行 activateMDM.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 因为没有它,提示符根本不会打开,但是添加这一行后,它只会短暂闪烁,不会保持打开状态。
如果我们在 BroadcastReceiver 开业 activity 在我们的应用程序中,然后 activity 打电话给 activateMDM 如上所述,我们实现了所需的功能。然而,拥有一个空的 activity 就是为了这个。如何编辑上述代码以使“激活设备管理器”提示以上述方式显示,以及为什么我们的代码仅使其闪烁而不保持打开状态?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题