Skip to content

Commit cdbf93e

Browse files
Merge pull request #28 from D4rK7355608/codex/fix-activitynotfoundexception-for-play-store-intent
Fix Play Store Intent crash
2 parents 786f3b7 + fc01d5f commit cdbf93e

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/home/repository/HomeRepository.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,28 @@ public HomeRepository(Context context) {
4343
*/
4444
public Intent getPlayStoreIntent() {
4545
String playStoreUrl = "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID;
46-
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(playStoreUrl));
47-
intent.setPackage("com.android.vending");
48-
return intent;
46+
return buildPlayStoreIntent(playStoreUrl);
4947
}
5048

5149
/**
5250
* Returns an Intent that opens the Google Play Store page for the provided package.
5351
*/
5452
public Intent getAppPlayStoreIntent(String packageName) {
5553
String url = "https://play.google.com/store/apps/details?id=" + packageName;
56-
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
57-
intent.setPackage("com.android.vending");
58-
return intent;
54+
return buildPlayStoreIntent(url);
55+
}
56+
57+
/**
58+
* Builds an intent that opens a Play Store url if the Google Play app is
59+
* installed, otherwise falls back to a generic ACTION_VIEW intent.
60+
*/
61+
private Intent buildPlayStoreIntent(String url) {
62+
Intent playStoreIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
63+
playStoreIntent.setPackage("com.android.vending");
64+
if (playStoreIntent.resolveActivity(context.getPackageManager()) != null) {
65+
return playStoreIntent;
66+
}
67+
return new Intent(Intent.ACTION_VIEW, Uri.parse(url));
5968
}
6069

6170
/**

0 commit comments

Comments
 (0)