android - 如果我不知道包名,如何以编程方式打开应用程序 android

标签 android

我知道要以编程方式打开应用程序,我们可以这样做:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.name");
startActivity(launchIntent);

但是不知道包名怎么办;只是应用程序名称?

谢谢!

编辑:我试图通过打开应用程序并检查日志来获取包名,但包名没有显示。

最佳答案

也许这样的事情会有所帮助,但我必须补充一点,这是一种糟糕的方法,因为不同应用程序的应用程序名称可能相同,并且某些应用程序(如“联系人”)根据语言集具有不同的标签

     /**
     *
     * @param appName The target app to launch, if there are multiple apps with same name, the first found will be launched
     * @throws IllegalArgumentException If the target app was not found
     */
    public void runApp(String appName) throws IllegalArgumentException {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        for ( ResolveInfo info : getPackageManager().queryIntentActivities( mainIntent, 0) ) {
            if ( info.loadLabel(getPackageManager()).equals(appName) ) {
                Intent launchIntent = getPackageManager().getLaunchIntentForPackage(info.activityInfo.applicationInfo.packageName);
                startActivity(launchIntent);
                return;
            }
        }
        throw new IllegalArgumentException("Application not found!");
    }

关于android - 如果我不知道包名,如何以编程方式打开应用程序 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30169390/

相关文章:

java - 试图让 Android 设备进入休眠状态,但 PowerManager 不包含“goToSleep(long) 方法

android - 仅使用 GCM 为 android 开发聊天应用程序放弃 XMPP 是否好?

android - AES/CBC/PKCS5Padding加密解密算法和SHA-1哈希算法是否适用于所有类型的Android设备

android - 如何在 Android 上获取 xml 文件的 "preview layout"?

android - 如何检测是否有人在特定 LinearLayout 之外单击

Android SQLite : nullColumnHack parameter in insert/replace methods

javascript - NFC 读取器 Apache · Cordova

android - 从 API 8 开始获取用户/所有者个人资料联系 URI 和用户图像

android - 如何关闭 Android Binder 线程?

android - 当我尝试一起使用grpc和cordova时,程序类型已经存在com.squareup.okhttp.address