Open third party app

2019-02-20 06:46发布

I am developing an app that uses packagenames to start a Third Party App. I have done some research and found out that all apps can be started from a launcher intent. Are there anyone that knows how to do this from a click of a Button.

3条回答
贪生不怕死
2楼-- · 2019-02-20 07:14

You can't really 'start applications'. You can try to get the Launch Intent from the 3rd party application if you know the packagename:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.thirdparty.package");
startActivity( intent );
查看更多
成全新的幸福
3楼-- · 2019-02-20 07:28

Just put it in an View.OnClickListener:

myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = getPackageManager().getLaunchIntentForPackage(theOtherActivityPackage);
        startActivity( intent );
    }
});
查看更多
狗以群分
4楼-- · 2019-02-20 07:35

For above accepted answer, if the third party app is not installed on your emulator, you should gracefully handle it also. Here is a complete code for the same:

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 
                }   

            }
        }

    }
查看更多
登录 后发表回答