打开第三方应用

Mag*_*ahn 1 android android-intent

我正在开发一个使用包名来启动第三方应用程序的应用程序.我做了一些研究,发现所有应用程序都可以从启动器意图启动.是否有人知道如何通过单击按钮来执行此操作.

Joh*_*lin 6

你无法真正"启动应用程序".如果您知道packagename,可以尝试从第三方应用程序获取Launch Intent:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.thirdparty.package");
startActivity( intent );
Run Code Online (Sandbox Code Playgroud)


YuD*_*oid 5

对于上述接受的答案,如果您的模拟器上未安装第三方应用程序,您也应该妥善处理它。这是相同的完整代码:

public void openThirdPartyApp() {   

        Intent intent = new Intent("com.thirdparty.package");
            intent.setPackage("com.thirdparty.package");

            try {
                ((Activity) context).startActivityForResult(intent, REQUEST_CODE);
            } catch (ActivityNotFoundException e) {
                downloadIt();
            }
        }

    private void downloadIt() {

    Uri uri = Uri.parse("market://search?q=pname:" + "com.thirdparty.package");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);

                try {
                    context.startActivity(intent);
                } catch (ActivityNotFoundException e) {
    //creates a small window to notify there is no app available 
                }   

            }
        }

    }
Run Code Online (Sandbox Code Playgroud)