Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

android - Launching an application if installed or redirect to Google Play

Until now I was using this code to launch a 3rd party application if installed or redirect to Google Play to download it if not installed already:

Intent intent = Intent intent = getPackageManager().getLaunchIntentForPackage("com.package.address");
if (intent != null)
{
/* we found the activity now start the activity */
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
/* bring user to the market or let them choose an app? */
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id="+"com.package.address"));
startActivity(intent);
}

Reading here I found the way to launch a particular activity instead (that was my desired result):

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new  ComponentName("com.package.address","com.package.address.xxxxxActivity"));
startActivity(intent);

It works launching the particular activity, but with my very very very limited knowledge I don't know how to modify the condition to launch the link to Google Play if the app isn't installed.

Hope someone can help me^^ Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use a try catch to see if the package name exists

try {
    PackageManager pm=getPackageManager();
    PackageInfo info=pm.getPackageInfo("com.package.address",PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
    //launch play store
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...