Xaren
Xaren

Reputation: 511

Intent for install app from app gallery or playstore

I want to install a companion app from app gallery or from playstore whether available on Huawei or non huawei devices.

Today my working code that opens playstore is:

val appId = "com.my.app.i.want.to.install"
val installAppMarketIntent = Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appId))
val installAppUrlIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id="+appId))
try {
    activity.startActivity(installAppMarketIntent)
}catch (anfe: android.content.ActivityNotFoundException){
    activity.startActivity(installAppUrlIntent)
}finally {
    activity!!.finish()
}

Should i add a validation for huawei devices? or this intent will work on Huawei devices with AppGallery?

Upvotes: 0

Views: 828

Answers (2)

zhangxaochen
zhangxaochen

Reputation: 33997

Yes,you should add a validation for Huawei devices.

But ("market://details?id=" + appId) intent is for Google Play.

Huawei AppGallery should be :

“appmarket://details?id=”+ pkgName (pkgName is the package name of the app)

or

“market://com.huawei.appmarket.applink?appId=” + appid

Upvotes: 1

Deep Rathod
Deep Rathod

Reputation: 69

For Kotlin

 val uri = Uri.parse("market://details?id=" + getPackageName())
                val goToMarket = Intent(Intent.ACTION_VIEW, uri)
                goToMarket.addFlags(
                    Intent.FLAG_ACTIVITY_NO_HISTORY or
                            Intent.FLAG_ACTIVITY_NEW_DOCUMENT or
                            Intent.FLAG_ACTIVITY_MULTIPLE_TASK
                )
                try {
                    startActivity(goToMarket)
                } catch (e: ActivityNotFoundException) {
                    startActivity(
                        Intent(
                            Intent.ACTION_VIEW,
                            Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())
                        )
                    )
                }

For Java

  public void openPlayStore(Context context) {
        // you can also use BuildConfig.APPLICATION_ID
        String appId = context.getPackageName();
        Intent playIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appId));
        final List<ResolveInfo> otherApps = context.getPackageManager().queryIntentActivities(playIntent, 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
                );
               playIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               playIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                playIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                playIntent.setComponent(componentName);
                context.startActivity(playIntent);

            }
        }
    }

Upvotes: 1

Related Questions