启用三星“双信使”后,如何以编程方式启动第二信使?

Eha*_*eda 5 android android-intent samsung-mobile whatsapp whatsapi

Dual Messenger是三星 Android 手机的一项功能

[三星A70(Android 9)截图]

例如,我可以通过调用包名称“com.whatsapp”来使用 Intent 启动 WhatsApp 的第一个副本。

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "THIS IS MY TEXT";

        PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);

        waIntent.setPackage("com.whatsapp");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(Intent.createChooser(waIntent, "Share with"));

Run Code Online (Sandbox Code Playgroud)

Intent启动第二个 WhatsApp 所需的软件包名称或其他选项是什么?

小智 0

三星似乎为双信使应用程序使用了单独的用户配置文件机制。要访问这些应用程序,我们可以从所有用户配置文件中收集应用程序列表,过滤掉我们已经拥有的应用程序。这将为我们留下双信使应用程序,然后我们可以使用 LauncherApps 服务启动它们。

这是我们获取所有应用程序列表(包括双信使应用程序)的方法

suspend fun getAppsList(context: Context): MutableList<App> {
  return withContext(Dispatchers.IO) {
    val appList: MutableList<App> = mutableListOf()

    try {
      val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager
      val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
      val collator = Collator.getInstance()

      for (profile in userManager.userProfiles) {
        for (app in launcherApps.getActivityList(null, profile)) {
          val appLabelShown = app.label.toString()
          val appModel = App(app.applicationInfo.packageName, appLabelShown, profile)

          val appAlreadyExists =
              appList.any { existingApp ->
                existingApp.packageName == appModel.packageName && existingApp.user == appModel.user
              }

          if (app.applicationInfo.packageName != BuildConfig.APPLICATION_ID && !appAlreadyExists) {
            appList.add(appModel)
          }
        }
      }
      appList.sortBy { it.appName.lowercase() }
    } catch (e: Exception) {
      e.printStackTrace()
    }
    appList
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我们启动应用程序的方式

private fun launchApp(packageName: String, userHandle: UserHandle) {
  val launcher = appContext.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps

  val activityInfo = launcher.getActivityList(packageName, userHandle)

  val component =
      when (activityInfo.size) {
        0 -> {
          // Handle app not found case
          return
        }
        1 -> ComponentName(packageName, activityInfo[0].name)
        else -> ComponentName(packageName, activityInfo.last().name)
      }

  try {
    launcher.startMainActivity(component, userHandle, null, null)
  } catch (e: SecurityException) {
    try {
      launcher.startMainActivity(component, android.os.Process.myUserHandle(), null, null)
    } catch (e: Exception) {
      // Handle unable to open app case
    }
  } catch (e: Exception) {
    // Handle unable to open app case
  }
}
Run Code Online (Sandbox Code Playgroud)

我在 Olauncher(Github)中找到了这个方法,它完全符合我想要的列表并启动所有应用程序,包括双信使应用程序