Related
I just linked some social networks to my app as a preliminary test and using the same kind of code, the results I am getting are different for Facebook, Instagram and Twitter intents.
When I click Facebook or Twitter, it opens the app automatically when it's installed and uses browser when it's not. However, that's not the case with Instagram. The complete action using dialog pops up and that's not something I want to happen.
protected void LaunchInstagram() {
String InstagramUsername = "USERNAME";
String LaunchInstagram = "http://instagram.com/_u/" + InstagramUsername;
String InstagramURL = "https://instagram.com/" + InstagramUsername;
try {
this.getPackageManager().getPackageInfo("com.instagram.android", 0);
Uri Uri = Uri.parse(LaunchInstagram);
startActivity(new Intent(Intent.ACTION_VIEW, Uri));
} catch (PackageManager.NameNotFoundException InstagramAppNotFoundOpenBrowser) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(InstagramURL)));
}
}
You are using
String InstagramURL = "https://instagram.com/" + InstagramUsername;
So every Intent you'll try to launch, will open a chooser because this is a URL.
than every App that supports URL, like browsers and such, will try to handle that.
EDIT 1:
Try it this way:
public static boolean openApp(Context context, String packageName) {
PackageManager manager = context.getPackageManager();
try {
Intent i = manager.getLaunchIntentForPackage(packageName);
if (i == null) {
return false;
//throw new PackageManager.NameNotFoundException();
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
I'm not sure if that will help you to open a specific user page on the App thou.
EDIT 2:
Try it this way:
Uri uri = Uri.parse("http://instagram.com/_u/USERNAME");
Intent insta = new Intent(Intent.ACTION_VIEW, uri);
insta.setPackage("com.instagram.android");
startActivity(insta);
I have open the Google Play store using the following code
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=my packagename "));
startActivity(i);.
But it shows me a Complete Action View as to select the option (browser/play store). I need to open the application in Play Store directly.
You can do this using the market:// prefix.
Java
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
Kotlin
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$packageName")))
} catch (e: ActivityNotFoundException) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageName")))
}
We use a try/catch block here because an Exception will be thrown if the Play Store is not installed on the target device.
NOTE: Any app can register as capable of handling the market://details?id=<appId> URI. If you want to specifically target Google Play, the solution in Berťák's answer is a good alternative.
Many answers here suggest to use Uri.parse("market://details?id=" + appPackageName)) to open Google Play, but I think it is insufficient in fact:
Some third-party applications can use its own intent-filters with "market://" scheme defined, thus they can process supplied Uri instead of Google Play (I experienced this situation with e.g.SnapPea application). The question is "How to open the Google Play Store?", so I assume, that you do not want to open any other application. Please also note, that e.g. app rating is only relevant in GP Store app etc...
To open Google Play AND ONLY Google Play I use this method:
public static void openAppRating(Context context) {
// you can also use BuildConfig.APPLICATION_ID
String appId = context.getPackageName();
Intent rateIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + appId));
boolean marketFound = false;
// find all applications able to handle our rateIntent
final List<ResolveInfo> otherApps = context.getPackageManager()
.queryIntentActivities(rateIntent, 0);
for (ResolveInfo otherApp: otherApps) {
// look for Google Play application
if (otherApp.activityInfo.applicationInfo.packageName
.equals("com.android.vending")) {
ActivityInfo otherAppActivity = otherApp.activityInfo;
ComponentName componentName = new ComponentName(
otherAppActivity.applicationInfo.packageName,
otherAppActivity.name
);
// make sure it does NOT open in the stack of your activity
rateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// task reparenting if needed
rateIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// if the Google Play was already open in a search result
// this make sure it still go to the app page you requested
rateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// this make sure only the Google Play app is allowed to
// intercept the intent
rateIntent.setComponent(componentName);
context.startActivity(rateIntent);
marketFound = true;
break;
}
}
// if GP not present on device, open web browser
if (!marketFound) {
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id="+appId));
context.startActivity(webIntent);
}
}
The point is that when more applications beside Google Play can open our intent, app-chooser dialog is skipped and GP app is started directly.
UPDATE:
Sometimes it seems that it opens GP app only, without opening the app's profile. As TrevorWiley suggested in his comment, Intent.FLAG_ACTIVITY_CLEAR_TOP could fix the problem. (I didn't test it myself yet...)
See this answer for understanding what Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED does.
Go on Android Developer official link as tutorial step by step see and got the code for your application package from play store if exists or play store apps not exists then open application from web browser.
Android Developer official link
https://developer.android.com/distribute/tools/promote/linking.html
Linking to a Application Page
From a web site: https://play.google.com/store/apps/details?id=<package_name>
From an Android app: market://details?id=<package_name>
Linking to a Product List
From a web site: https://play.google.com/store/search?q=pub:<publisher_name>
From an Android app: market://search?q=pub:<publisher_name>
Linking to a Search Result
From a web site: https://play.google.com/store/search?q=<search_query>&c=apps
From an Android app: market://search?q=<seach_query>&c=apps
While Eric's answer is correct and Berťák's code also works. I think this combines both more elegantly.
try {
Intent appStoreIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
appStoreIntent.setPackage("com.android.vending");
startActivity(appStoreIntent);
} catch (android.content.ActivityNotFoundException exception) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
By using setPackage, you force the device to use the Play Store. If there is no Play Store installed, the Exception will be caught.
try this
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);
All of the above answers open Google Play in a new view of the same app, if you actually want to open Google Play (or any other app) independently:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.vending");
// package name and activity
ComponentName comp = new ComponentName("com.android.vending",
"com.google.android.finsky.activities.LaunchUrlHandlerActivity");
launchIntent.setComponent(comp);
// sample to open facebook app
launchIntent.setData(Uri.parse("market://details?id=com.facebook.katana"));
startActivity(launchIntent);
The important part is that actually opens google play or any other app independently.
Most of what I have seen uses the approach of the other answers and it was not what I needed hopefully this helps somebody.
Regards.
You can check if the Google Play Store app is installed and, if this is the case, you can use the "market://" protocol.
final String my_package_name = "........." // <- HERE YOUR PACKAGE NAME!!
String url = "";
try {
//Check whether Google Play store is installed or not:
this.getPackageManager().getPackageInfo("com.android.vending", 0);
url = "market://details?id=" + my_package_name;
} catch ( final Exception e ) {
url = "https://play.google.com/store/apps/details?id=" + my_package_name;
}
//Open the app page in Google Play store:
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(intent);
use market://
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + my_packagename));
As the official docs use https:// instead of market://, this combines Eric's and M3-n50's answer with code reuse (don't repeat yourself):
Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
try {
startActivity(new Intent(intent)
.setPackage("com.android.vending"));
} catch (android.content.ActivityNotFoundException exception) {
startActivity(intent);
}
It tries to open with the GPlay app if it exists and falls back to default.
Some of the answers to this question are outdated.
What worked for me (in 2020) was to explicitly tell the intent to skip the chooser and directly open the play store app, according to this link:
"If you want to link to your products from an Android app, create an
Intent that opens a URL. As you configure this intent, pass
"com.android.vending" into Intent.setPackage() so that users see your
app's details in the Google Play Store app instead of a chooser."
This is the Kotlin code I used to direct users to viewing the app containing the package name com.google.android.apps.maps in Google Play:
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse("http://play.google.com/store/apps/details?id=com.google.android.apps.maps")
setPackage("com.android.vending")
}
startActivity(intent)
I hope that helps someone!
Kotlin:
Extension:
fun Activity.openAppInGooglePlay(){
val appId = BuildConfig.APPLICATION_ID
try {
this.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId")))
} catch (anfe: ActivityNotFoundException) {
this.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$appId")
)
)
}}
Method:
fun openAppInGooglePlay(activity:Activity){
val appId = BuildConfig.APPLICATION_ID
try {
activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId")))
} catch (anfe: ActivityNotFoundException) {
activity.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$appId")
)
)
}
}
You can do:
final Uri marketUri = Uri.parse("market://details?id=" + packageName);
startActivity(new Intent(Intent.ACTION_VIEW, marketUri));
get Reference here:
You can also try the approach described in the accepted answer of this question:
Cannot determine whether Google play store is installed or not on Android device
Very late in the party Official docs are here. And code described is
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
"https://play.google.com/store/apps/details?id=com.example.android"));
intent.setPackage("com.android.vending");
startActivity(intent);
As you configure this intent, pass "com.android.vending" into Intent.setPackage() so that users see your app's details in the Google Play Store app instead of a chooser.
for KOTLIN
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(
"https://play.google.com/store/apps/details?id=com.example.android")
setPackage("com.android.vending")
}
startActivity(intent)
If you have published an instant app using Google Play Instant, you can launch the app as follows:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri.Builder uriBuilder = Uri.parse("https://play.google.com/store/apps/details")
.buildUpon()
.appendQueryParameter("id", "com.example.android")
.appendQueryParameter("launch", "true");
// Optional parameters, such as referrer, are passed onto the launched
// instant app. You can retrieve these parameters using
// Activity.getIntent().getData().
uriBuilder.appendQueryParameter("referrer", "exampleCampaignId");
intent.setData(uriBuilder.build());
intent.setPackage("com.android.vending");
startActivity(intent);
For KOTLIN
val uriBuilder = Uri.parse("https://play.google.com/store/apps/details")
.buildUpon()
.appendQueryParameter("id", "com.example.android")
.appendQueryParameter("launch", "true")
// Optional parameters, such as referrer, are passed onto the launched
// instant app. You can retrieve these parameters using Activity.intent.data.
uriBuilder.appendQueryParameter("referrer", "exampleCampaignId")
val intent = Intent(Intent.ACTION_VIEW).apply {
data = uriBuilder.build()
setPackage("com.android.vending")
}
startActivity(intent)
Ready-to-use solution:
public class GoogleServicesUtils {
public static void openAppInGooglePlay(Context context) {
final String appPackageName = context.getPackageName();
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException e) { // if there is no Google Play on device
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
}
Based on Eric's answer.
Kotlin
fun openAppInPlayStore(appPackageName: String) {
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appPackageName")))
} catch (exception: android.content.ActivityNotFoundException) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName")))
}
}
This link will open the app automatically in market:// if you are on Android and in browser if you are on PC.
https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.app.id&ddl=1&pcampaignid=web_ddl_1
If you want to open Google Play store from your app then use this command directy: market://details?gotohome=com.yourAppName, it will open your app's Google Play store pages.
Web: http://play.google.com/store/apps/details?id=
App: market://details?id=
Show all apps by a specific publisher
Web: http://play.google.com/store/search?q=pub:
App: market://search?q=pub:
Search for apps that using the Query on its title or description
Web: http://play.google.com/store/search?q=
App: market://search?q=
Reference: https://tricklio.com/market-details-gotohome-1/
Here is the final code from the answers above that first attempts to open the app using the Google play store app and specifically play store, if it fails, it will start the action view using the web version:
Credits to #Eric, #Jonathan Caballero
public void goToPlayStore() {
String playStoreMarketUrl = "market://details?id=";
String playStoreWebUrl = "https://play.google.com/store/apps/details?id=";
String packageName = getActivity().getPackageName();
try {
Intent intent = getActivity()
.getPackageManager()
.getLaunchIntentForPackage("com.android.vending");
if (intent != null) {
ComponentName androidComponent = new ComponentName("com.android.vending",
"com.google.android.finsky.activities.LaunchUrlHandlerActivity");
intent.setComponent(androidComponent);
intent.setData(Uri.parse(playStoreMarketUrl + packageName));
} else {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(playStoreMarketUrl + packageName));
}
startActivity(intent);
} catch (ActivityNotFoundException e) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(playStoreWebUrl + packageName));
startActivity(intent);
}
}
I have combined both Berťák and Stefano Munarini answer to creating a hybrid solution which handles both Rate this App and Show More App scenario.
/**
* This method checks if GooglePlay is installed or not on the device and accordingly handle
* Intents to view for rate App or Publisher's Profile
*
* #param showPublisherProfile pass true if you want to open Publisher Page else pass false to open APp page
* #param publisherID pass Dev ID if you have passed PublisherProfile true
*/
public void openPlayStore(boolean showPublisherProfile, String publisherID) {
//Error Handling
if (publisherID == null || !publisherID.isEmpty()) {
publisherID = "";
//Log and continue
Log.w("openPlayStore Method", "publisherID is invalid");
}
Intent openPlayStoreIntent;
boolean isGooglePlayInstalled = false;
if (showPublisherProfile) {
//Open Publishers Profile on PlayStore
openPlayStoreIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://search?q=pub:" + publisherID));
} else {
//Open this App on PlayStore
openPlayStoreIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + getPackageName()));
}
// find all applications who can handle openPlayStoreIntent
final List<ResolveInfo> otherApps = getPackageManager()
.queryIntentActivities(openPlayStoreIntent, 0);
for (ResolveInfo otherApp : otherApps) {
// look for Google Play application
if (otherApp.activityInfo.applicationInfo.packageName.equals("com.android.vending")) {
ActivityInfo otherAppActivity = otherApp.activityInfo;
ComponentName componentName = new ComponentName(
otherAppActivity.applicationInfo.packageName,
otherAppActivity.name
);
// make sure it does NOT open in the stack of your activity
openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// task reparenting if needed
openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// if the Google Play was already open in a search result
// this make sure it still go to the app page you requested
openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// this make sure only the Google Play app is allowed to
// intercept the intent
openPlayStoreIntent.setComponent(componentName);
startActivity(openPlayStoreIntent);
isGooglePlayInstalled = true;
break;
}
}
// if Google Play is not Installed on the device, open web browser
if (!isGooglePlayInstalled) {
Intent webIntent;
if (showPublisherProfile) {
//Open Publishers Profile on web browser
webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/search?q=pub:" + getPackageName()));
} else {
//Open this App on web browser
webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
}
startActivity(webIntent);
}
}
Usage
To Open Publishers Profile
#OnClick(R.id.ll_more_apps)
public void showMoreApps() {
openPlayStore(true, "Hitesh Sahu");
}
To Open App Page on PlayStore
#OnClick(R.id.ll_rate_this_app)
public void openAppInPlayStore() {
openPlayStore(false, "");
}
public void launchPlayStore(Context context, String packageName) {
Intent intent = null;
try {
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + packageName));
context.startActivity(intent);
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)));
}
}
My kotlin entension function for this purpose
fun Context.canPerformIntent(intent: Intent): Boolean {
val mgr = this.packageManager
val list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
return list.size > 0
}
And in your activity
val uri = if (canPerformIntent(Intent(Intent.ACTION_VIEW, Uri.parse("market://")))) {
Uri.parse("market://details?id=" + appPackageName)
} else {
Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)
}
startActivity(Intent(Intent.ACTION_VIEW, uri))
KOTLIN :
create extension in context.
fun Context.openPlayStoreApp(pkgName:String?){
if(!pkgName.isNullOrEmpty()) {
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$pkgName")))
} catch (e: ActivityNotFoundException) {
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$pkgName")
)
)
}
}
}
Hope it should work.
Peoples, dont forget that you could actually get something more from it. I mean UTM tracking for example. https://developers.google.com/analytics/devguides/collection/android/v4/campaigns
public static final String MODULE_ICON_PACK_FREE = "com.example.iconpack_free";
public static final String APP_STORE_URI =
"market://details?id=%s&referrer=utm_source=%s&utm_medium=app&utm_campaign=plugin";
public static final String APP_STORE_GENERIC_URI =
"https://play.google.com/store/apps/details?id=%s&referrer=utm_source=%s&utm_medium=app&utm_campaign=plugin";
try {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse(String.format(Locale.US,
APP_STORE_URI,
MODULE_ICON_PACK_FREE,
getPackageName()))).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse(String.format(Locale.US,
APP_STORE_GENERIC_URI,
MODULE_ICON_PACK_FREE,
getPackageName()))).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
A kotlin verison with fallback and current syntax
fun openAppInPlayStore() {
val uri = Uri.parse("market://details?id=" + context.packageName)
val goToMarketIntent = Intent(Intent.ACTION_VIEW, uri)
var flags = Intent.FLAG_ACTIVITY_NO_HISTORY or Intent.FLAG_ACTIVITY_MULTIPLE_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
flags = if (Build.VERSION.SDK_INT >= 21) {
flags or Intent.FLAG_ACTIVITY_NEW_DOCUMENT
} else {
flags or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
goToMarketIntent.addFlags(flags)
try {
startActivity(context, goToMarketIntent, null)
} catch (e: ActivityNotFoundException) {
val intent = Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + context.packageName))
startActivity(context, intent, null)
}
}
Tested. This should work fine.
val context = LocalContext.current
val onOpenPlayStore: () -> Unit = {
try {
LOG.d(tag, "onOpenPlayStore ${context.packageName}")
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=${context.packageName}"))
startActivity(context, intent, null)
} catch (e: ActivityNotFoundException) {
var intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=${context.packageName}"))
startActivity(context, intent, null)
}
}
For Rate Application: Redirect to Playstore.
In Flutter, you can do it through a Platform channel Like this
Flutter Part:-
static const platform = const MethodChannel('rateApp'); // initialize
onTap: platform.invokeMethod('urls', {'android_id': 'com.xyz'}),
Now Android Native Part(Java):
private static final String RATEAPP = "rateApp"; // initialize variable
// Now in ConfigureFlutterEngine funtion:
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), RATEAPP)
.setMethodCallHandler(
(call, result) -> {
if (call.method.equals("urls") && call.hasArgument("android_id")) {
String id = call.argument("android_id").toString();
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("$uri" + id)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + id)));
}
result.success("Done");
} else {
result.notImplemented();
}
}
);
I used to open my facebook page from my app using the below code, but this does not work anymore starting facebook v11.0.0.11.23 released on June 21, 2014, any idea how to open the page in the new facebook app?
To note that it opens now the facebook app but without opening the specified page, it used to work just fine before the latest update
public void openFacebookPage() {
Intent intent = null;
try {
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/<id here>"));
//tried this also
//intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/<id here>"));
} catch (Exception e) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<name here>"));
}
context.startActivity(intent);
}
In Facebook version 11.0.0.11.23 (3002850) fb://profile/ and fb://page/ are no longer supported. I decompiled the Facebook app and was able to come up with the following solution:
String facebookUrl = "https://www.facebook.com/JRummyApps";
try {
int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
if (versionCode >= 3002850) {
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));;
} else {
// open the Facebook app using the old method (fb://profile/id or fb://page/id)
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/336227679757310")));
}
} catch (PackageManager.NameNotFoundException e) {
// Facebook is not installed. Open the browser
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookUrl)));
}
Edit: It has been some time and it looks like fb://profile and fb://page are no longer supported. Below is the method I have been using in production:
/**
* Intent to open the official Facebook app. If the Facebook app is not installed then the
* default web browser will be used.</p>
*
* Example usage:</p>
* <code>newFacebookIntent(context.getPackageManager(), "https://www.facebook.com/JRummyApps");</code></p>
*
* #param pm
* Instance of the {#link PackageManager}.
* #param url
* The full URL to the Facebook page or profile.
* #return An intent that will open the Facebook page/profile.
*/
public static Intent newFacebookIntent(PackageManager pm, String url) {
Uri uri;
try {
pm.getPackageInfo("com.facebook.katana", 0);
// http://stackoverflow.com/a/24547437/1048340
uri = Uri.parse("fb://facewebmodal/f?href=" + url);
} catch (PackageManager.NameNotFoundException e) {
uri = Uri.parse(url);
}
return new Intent(Intent.ACTION_VIEW, uri);
}
I managed to open the Twitter and Facebook user profile from my app. But I can not find any references to Instagram.
Is There a way to open Instagram in order to show a user profile like in twitter or facebook?
For instance, in order to get the Intent to launch the twitter application I do:
public Intent getOpenTwitterIntent(Context context) {
try {
return new Intent(Intent.ACTION_VIEW,
Uri.parse("twitter://user?screen_name="
.concat(twitterUsername)));
} catch (Exception e) {
return new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/#!/".concat(twitterUsername)));
}
}
How can I achive something similar with Instagram?
Thanks in advance.
Here is a method to get the Intent to open the Instagram app to the user's profile page:
/**
* Intent to open the official Instagram app to the user's profile. If the Instagram app is not
* installed then the Web Browser will be used.</p>
*
* Example usage:</p> {#code newInstagramProfileIntent(context.getPackageManager(),
* "http://instagram.com/jaredrummler");}</p>
*
* #param pm
* The {#link PackageManager}. You can find this class through
* {#link Context#getPackageManager()}.
* #param url
* The URL to the user's Instagram profile.
* #return The intent to open the Instagram app to the user's profile.
*/
public static Intent newInstagramProfileIntent(PackageManager pm, String url) {
final Intent intent = new Intent(Intent.ACTION_VIEW);
try {
if (pm.getPackageInfo("com.instagram.android", 0) != null) {
if (url.endsWith("/")) {
url = url.substring(0, url.length() - 1);
}
final String username = url.substring(url.lastIndexOf("/") + 1);
// http://stackoverflow.com/questions/21505941/intent-to-open-instagram-user-profile-on-android
intent.setData(Uri.parse("http://instagram.com/_u/" + username));
intent.setPackage("com.instagram.android");
return intent;
}
} catch (NameNotFoundException ignored) {
}
intent.setData(Uri.parse(url));
return intent;
}
So far I couldn't find a way to show user's profile directly.. but there is a way around..
Found this solution from Instagram Manifest from GitHub
Intent iIntent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
iIntent.setComponent(new ComponentName( "com.instagram.android", "com.instagram.android.activity.UrlHandlerActivity"));
iIntent.setData( Uri.parse( "http://instagram.com/p/gjfLqSBQTJ/") );
startActivity(iIntent);
This will open particular image post by the user in the app. From the page app user can open the profile with the link inside.
If you want to open recent post or something , you can use Instagram API
This is not we want exactly , but the better option now... i guess :)
After a brief search and trying what we all know "WON'T WORK" I got this to work
Uri uri = Uri.parse("http://instagram.com/mzcoco2you");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
This should start your default browser and ... there you go. The answer is "instagram.com" + "/UserName".
To open directly instagram app to a user profile :
String scheme = "http://instagram.com/_u/USER";
String path = "https://instagram.com/USER";
String nomPackageInfo ="com.instagram.android";
try {
activite.getPackageManager().getPackageInfo(nomPackageInfo, 0);
intentAiguilleur = new Intent(Intent.ACTION_VIEW, Uri.parse(scheme));
} catch (Exception e) {
intentAiguilleur = new Intent(Intent.ACTION_VIEW, Uri.parse(path));
}
activite.startActivity(intentAiguilleur);
// Use this link to open directly a picture
String scheme = "http://instagram.com/_p/PICTURE";
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://instagram.com/_u/" + "username"));
intent.setPackage("com.instagram.android");
startActivity(intent);
}
catch (android.content.ActivityNotFoundException anfe)
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.instagram.com/" + username)));
}
Unfortunately, at this time you cannot open the Instagram app to go directly to a user profile. You can however open a certain photo in the Instagram app.
The following code that I have provided will open a certain photo inside of the Instagram app. If no Instagram app is installed, it will open a user profile inside of the browser.
public void openInstagram (View view) {
Intent intent = null;
String pictureId = "p/0vtNxgqHx9";
String pageName = "d4v3_r34d";
try {intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
intent.setComponent(new ComponentName("com.instagram.android", "com.instagram.android.activity.UrlHandlerActivity"));
intent.setData(Uri.parse("http://instagram.com/" + pictureId));
} catch (Exception e) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/" + pageName));
}
this.startActivity(intent);
I made it very easy for this code to be edited for your own purposes. Set the "String pictureId" to the id of the picture that you want to display and set the "String pageName" to the user name of your Instagram account.
The user name part is obvious but if you need help to get your picture id look at this picture.
Check the code below:
Intent instaintent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
instaintent.setData(Uri.parse("https://www.instagram.com/insta_save_new/"));
startActivity(instaintent);
I have open the Google Play store using the following code
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=my packagename "));
startActivity(i);.
But it shows me a Complete Action View as to select the option (browser/play store). I need to open the application in Play Store directly.
You can do this using the market:// prefix.
Java
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
Kotlin
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$packageName")))
} catch (e: ActivityNotFoundException) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageName")))
}
We use a try/catch block here because an Exception will be thrown if the Play Store is not installed on the target device.
NOTE: Any app can register as capable of handling the market://details?id=<appId> URI. If you want to specifically target Google Play, the solution in Berťák's answer is a good alternative.
Many answers here suggest to use Uri.parse("market://details?id=" + appPackageName)) to open Google Play, but I think it is insufficient in fact:
Some third-party applications can use its own intent-filters with "market://" scheme defined, thus they can process supplied Uri instead of Google Play (I experienced this situation with e.g.SnapPea application). The question is "How to open the Google Play Store?", so I assume, that you do not want to open any other application. Please also note, that e.g. app rating is only relevant in GP Store app etc...
To open Google Play AND ONLY Google Play I use this method:
public static void openAppRating(Context context) {
// you can also use BuildConfig.APPLICATION_ID
String appId = context.getPackageName();
Intent rateIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + appId));
boolean marketFound = false;
// find all applications able to handle our rateIntent
final List<ResolveInfo> otherApps = context.getPackageManager()
.queryIntentActivities(rateIntent, 0);
for (ResolveInfo otherApp: otherApps) {
// look for Google Play application
if (otherApp.activityInfo.applicationInfo.packageName
.equals("com.android.vending")) {
ActivityInfo otherAppActivity = otherApp.activityInfo;
ComponentName componentName = new ComponentName(
otherAppActivity.applicationInfo.packageName,
otherAppActivity.name
);
// make sure it does NOT open in the stack of your activity
rateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// task reparenting if needed
rateIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// if the Google Play was already open in a search result
// this make sure it still go to the app page you requested
rateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// this make sure only the Google Play app is allowed to
// intercept the intent
rateIntent.setComponent(componentName);
context.startActivity(rateIntent);
marketFound = true;
break;
}
}
// if GP not present on device, open web browser
if (!marketFound) {
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id="+appId));
context.startActivity(webIntent);
}
}
The point is that when more applications beside Google Play can open our intent, app-chooser dialog is skipped and GP app is started directly.
UPDATE:
Sometimes it seems that it opens GP app only, without opening the app's profile. As TrevorWiley suggested in his comment, Intent.FLAG_ACTIVITY_CLEAR_TOP could fix the problem. (I didn't test it myself yet...)
See this answer for understanding what Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED does.
Go on Android Developer official link as tutorial step by step see and got the code for your application package from play store if exists or play store apps not exists then open application from web browser.
Android Developer official link
https://developer.android.com/distribute/tools/promote/linking.html
Linking to a Application Page
From a web site: https://play.google.com/store/apps/details?id=<package_name>
From an Android app: market://details?id=<package_name>
Linking to a Product List
From a web site: https://play.google.com/store/search?q=pub:<publisher_name>
From an Android app: market://search?q=pub:<publisher_name>
Linking to a Search Result
From a web site: https://play.google.com/store/search?q=<search_query>&c=apps
From an Android app: market://search?q=<seach_query>&c=apps
While Eric's answer is correct and Berťák's code also works. I think this combines both more elegantly.
try {
Intent appStoreIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
appStoreIntent.setPackage("com.android.vending");
startActivity(appStoreIntent);
} catch (android.content.ActivityNotFoundException exception) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
By using setPackage, you force the device to use the Play Store. If there is no Play Store installed, the Exception will be caught.
try this
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);
All of the above answers open Google Play in a new view of the same app, if you actually want to open Google Play (or any other app) independently:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.vending");
// package name and activity
ComponentName comp = new ComponentName("com.android.vending",
"com.google.android.finsky.activities.LaunchUrlHandlerActivity");
launchIntent.setComponent(comp);
// sample to open facebook app
launchIntent.setData(Uri.parse("market://details?id=com.facebook.katana"));
startActivity(launchIntent);
The important part is that actually opens google play or any other app independently.
Most of what I have seen uses the approach of the other answers and it was not what I needed hopefully this helps somebody.
Regards.
You can check if the Google Play Store app is installed and, if this is the case, you can use the "market://" protocol.
final String my_package_name = "........." // <- HERE YOUR PACKAGE NAME!!
String url = "";
try {
//Check whether Google Play store is installed or not:
this.getPackageManager().getPackageInfo("com.android.vending", 0);
url = "market://details?id=" + my_package_name;
} catch ( final Exception e ) {
url = "https://play.google.com/store/apps/details?id=" + my_package_name;
}
//Open the app page in Google Play store:
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(intent);
use market://
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + my_packagename));
As the official docs use https:// instead of market://, this combines Eric's and M3-n50's answer with code reuse (don't repeat yourself):
Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
try {
startActivity(new Intent(intent)
.setPackage("com.android.vending"));
} catch (android.content.ActivityNotFoundException exception) {
startActivity(intent);
}
It tries to open with the GPlay app if it exists and falls back to default.
Some of the answers to this question are outdated.
What worked for me (in 2020) was to explicitly tell the intent to skip the chooser and directly open the play store app, according to this link:
"If you want to link to your products from an Android app, create an
Intent that opens a URL. As you configure this intent, pass
"com.android.vending" into Intent.setPackage() so that users see your
app's details in the Google Play Store app instead of a chooser."
This is the Kotlin code I used to direct users to viewing the app containing the package name com.google.android.apps.maps in Google Play:
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse("http://play.google.com/store/apps/details?id=com.google.android.apps.maps")
setPackage("com.android.vending")
}
startActivity(intent)
I hope that helps someone!
Kotlin:
Extension:
fun Activity.openAppInGooglePlay(){
val appId = BuildConfig.APPLICATION_ID
try {
this.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId")))
} catch (anfe: ActivityNotFoundException) {
this.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$appId")
)
)
}}
Method:
fun openAppInGooglePlay(activity:Activity){
val appId = BuildConfig.APPLICATION_ID
try {
activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId")))
} catch (anfe: ActivityNotFoundException) {
activity.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$appId")
)
)
}
}
You can do:
final Uri marketUri = Uri.parse("market://details?id=" + packageName);
startActivity(new Intent(Intent.ACTION_VIEW, marketUri));
get Reference here:
You can also try the approach described in the accepted answer of this question:
Cannot determine whether Google play store is installed or not on Android device
Very late in the party Official docs are here. And code described is
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
"https://play.google.com/store/apps/details?id=com.example.android"));
intent.setPackage("com.android.vending");
startActivity(intent);
As you configure this intent, pass "com.android.vending" into Intent.setPackage() so that users see your app's details in the Google Play Store app instead of a chooser.
for KOTLIN
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(
"https://play.google.com/store/apps/details?id=com.example.android")
setPackage("com.android.vending")
}
startActivity(intent)
If you have published an instant app using Google Play Instant, you can launch the app as follows:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri.Builder uriBuilder = Uri.parse("https://play.google.com/store/apps/details")
.buildUpon()
.appendQueryParameter("id", "com.example.android")
.appendQueryParameter("launch", "true");
// Optional parameters, such as referrer, are passed onto the launched
// instant app. You can retrieve these parameters using
// Activity.getIntent().getData().
uriBuilder.appendQueryParameter("referrer", "exampleCampaignId");
intent.setData(uriBuilder.build());
intent.setPackage("com.android.vending");
startActivity(intent);
For KOTLIN
val uriBuilder = Uri.parse("https://play.google.com/store/apps/details")
.buildUpon()
.appendQueryParameter("id", "com.example.android")
.appendQueryParameter("launch", "true")
// Optional parameters, such as referrer, are passed onto the launched
// instant app. You can retrieve these parameters using Activity.intent.data.
uriBuilder.appendQueryParameter("referrer", "exampleCampaignId")
val intent = Intent(Intent.ACTION_VIEW).apply {
data = uriBuilder.build()
setPackage("com.android.vending")
}
startActivity(intent)
Ready-to-use solution:
public class GoogleServicesUtils {
public static void openAppInGooglePlay(Context context) {
final String appPackageName = context.getPackageName();
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException e) { // if there is no Google Play on device
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
}
Based on Eric's answer.
Kotlin
fun openAppInPlayStore(appPackageName: String) {
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appPackageName")))
} catch (exception: android.content.ActivityNotFoundException) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName")))
}
}
This link will open the app automatically in market:// if you are on Android and in browser if you are on PC.
https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.app.id&ddl=1&pcampaignid=web_ddl_1
If you want to open Google Play store from your app then use this command directy: market://details?gotohome=com.yourAppName, it will open your app's Google Play store pages.
Web: http://play.google.com/store/apps/details?id=
App: market://details?id=
Show all apps by a specific publisher
Web: http://play.google.com/store/search?q=pub:
App: market://search?q=pub:
Search for apps that using the Query on its title or description
Web: http://play.google.com/store/search?q=
App: market://search?q=
Reference: https://tricklio.com/market-details-gotohome-1/
Here is the final code from the answers above that first attempts to open the app using the Google play store app and specifically play store, if it fails, it will start the action view using the web version:
Credits to #Eric, #Jonathan Caballero
public void goToPlayStore() {
String playStoreMarketUrl = "market://details?id=";
String playStoreWebUrl = "https://play.google.com/store/apps/details?id=";
String packageName = getActivity().getPackageName();
try {
Intent intent = getActivity()
.getPackageManager()
.getLaunchIntentForPackage("com.android.vending");
if (intent != null) {
ComponentName androidComponent = new ComponentName("com.android.vending",
"com.google.android.finsky.activities.LaunchUrlHandlerActivity");
intent.setComponent(androidComponent);
intent.setData(Uri.parse(playStoreMarketUrl + packageName));
} else {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(playStoreMarketUrl + packageName));
}
startActivity(intent);
} catch (ActivityNotFoundException e) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(playStoreWebUrl + packageName));
startActivity(intent);
}
}
I have combined both Berťák and Stefano Munarini answer to creating a hybrid solution which handles both Rate this App and Show More App scenario.
/**
* This method checks if GooglePlay is installed or not on the device and accordingly handle
* Intents to view for rate App or Publisher's Profile
*
* #param showPublisherProfile pass true if you want to open Publisher Page else pass false to open APp page
* #param publisherID pass Dev ID if you have passed PublisherProfile true
*/
public void openPlayStore(boolean showPublisherProfile, String publisherID) {
//Error Handling
if (publisherID == null || !publisherID.isEmpty()) {
publisherID = "";
//Log and continue
Log.w("openPlayStore Method", "publisherID is invalid");
}
Intent openPlayStoreIntent;
boolean isGooglePlayInstalled = false;
if (showPublisherProfile) {
//Open Publishers Profile on PlayStore
openPlayStoreIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://search?q=pub:" + publisherID));
} else {
//Open this App on PlayStore
openPlayStoreIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + getPackageName()));
}
// find all applications who can handle openPlayStoreIntent
final List<ResolveInfo> otherApps = getPackageManager()
.queryIntentActivities(openPlayStoreIntent, 0);
for (ResolveInfo otherApp : otherApps) {
// look for Google Play application
if (otherApp.activityInfo.applicationInfo.packageName.equals("com.android.vending")) {
ActivityInfo otherAppActivity = otherApp.activityInfo;
ComponentName componentName = new ComponentName(
otherAppActivity.applicationInfo.packageName,
otherAppActivity.name
);
// make sure it does NOT open in the stack of your activity
openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// task reparenting if needed
openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// if the Google Play was already open in a search result
// this make sure it still go to the app page you requested
openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// this make sure only the Google Play app is allowed to
// intercept the intent
openPlayStoreIntent.setComponent(componentName);
startActivity(openPlayStoreIntent);
isGooglePlayInstalled = true;
break;
}
}
// if Google Play is not Installed on the device, open web browser
if (!isGooglePlayInstalled) {
Intent webIntent;
if (showPublisherProfile) {
//Open Publishers Profile on web browser
webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/search?q=pub:" + getPackageName()));
} else {
//Open this App on web browser
webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
}
startActivity(webIntent);
}
}
Usage
To Open Publishers Profile
#OnClick(R.id.ll_more_apps)
public void showMoreApps() {
openPlayStore(true, "Hitesh Sahu");
}
To Open App Page on PlayStore
#OnClick(R.id.ll_rate_this_app)
public void openAppInPlayStore() {
openPlayStore(false, "");
}
public void launchPlayStore(Context context, String packageName) {
Intent intent = null;
try {
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + packageName));
context.startActivity(intent);
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)));
}
}
My kotlin entension function for this purpose
fun Context.canPerformIntent(intent: Intent): Boolean {
val mgr = this.packageManager
val list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
return list.size > 0
}
And in your activity
val uri = if (canPerformIntent(Intent(Intent.ACTION_VIEW, Uri.parse("market://")))) {
Uri.parse("market://details?id=" + appPackageName)
} else {
Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)
}
startActivity(Intent(Intent.ACTION_VIEW, uri))
KOTLIN :
create extension in context.
fun Context.openPlayStoreApp(pkgName:String?){
if(!pkgName.isNullOrEmpty()) {
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$pkgName")))
} catch (e: ActivityNotFoundException) {
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$pkgName")
)
)
}
}
}
Hope it should work.
Peoples, dont forget that you could actually get something more from it. I mean UTM tracking for example. https://developers.google.com/analytics/devguides/collection/android/v4/campaigns
public static final String MODULE_ICON_PACK_FREE = "com.example.iconpack_free";
public static final String APP_STORE_URI =
"market://details?id=%s&referrer=utm_source=%s&utm_medium=app&utm_campaign=plugin";
public static final String APP_STORE_GENERIC_URI =
"https://play.google.com/store/apps/details?id=%s&referrer=utm_source=%s&utm_medium=app&utm_campaign=plugin";
try {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse(String.format(Locale.US,
APP_STORE_URI,
MODULE_ICON_PACK_FREE,
getPackageName()))).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse(String.format(Locale.US,
APP_STORE_GENERIC_URI,
MODULE_ICON_PACK_FREE,
getPackageName()))).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
A kotlin verison with fallback and current syntax
fun openAppInPlayStore() {
val uri = Uri.parse("market://details?id=" + context.packageName)
val goToMarketIntent = Intent(Intent.ACTION_VIEW, uri)
var flags = Intent.FLAG_ACTIVITY_NO_HISTORY or Intent.FLAG_ACTIVITY_MULTIPLE_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
flags = if (Build.VERSION.SDK_INT >= 21) {
flags or Intent.FLAG_ACTIVITY_NEW_DOCUMENT
} else {
flags or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
goToMarketIntent.addFlags(flags)
try {
startActivity(context, goToMarketIntent, null)
} catch (e: ActivityNotFoundException) {
val intent = Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + context.packageName))
startActivity(context, intent, null)
}
}
Tested. This should work fine.
val context = LocalContext.current
val onOpenPlayStore: () -> Unit = {
try {
LOG.d(tag, "onOpenPlayStore ${context.packageName}")
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=${context.packageName}"))
startActivity(context, intent, null)
} catch (e: ActivityNotFoundException) {
var intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=${context.packageName}"))
startActivity(context, intent, null)
}
}
For Rate Application: Redirect to Playstore.
In Flutter, you can do it through a Platform channel Like this
Flutter Part:-
static const platform = const MethodChannel('rateApp'); // initialize
onTap: platform.invokeMethod('urls', {'android_id': 'com.xyz'}),
Now Android Native Part(Java):
private static final String RATEAPP = "rateApp"; // initialize variable
// Now in ConfigureFlutterEngine funtion:
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), RATEAPP)
.setMethodCallHandler(
(call, result) -> {
if (call.method.equals("urls") && call.hasArgument("android_id")) {
String id = call.argument("android_id").toString();
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("$uri" + id)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + id)));
}
result.success("Done");
} else {
result.notImplemented();
}
}
);