Skrew
Skrew

Reputation: 1847

Is it possible to detect and launch app A if present, if not then B, if neither are present redirect to store?

I'd like to have some kind of prioritized app launch from a browser on android. Goal would be to : 1. launch app A if present 2. If not, but app B is present launch app B 3. If neither : redirect to app A in google play store

Thanks.

Upvotes: 0

Views: 63

Answers (2)

Rajan Kadeval
Rajan Kadeval

Reputation: 329

Yes it is possible. First of all make you app browsable via intent.

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" /> 
<data android:scheme="http" android:host="yourapp.com" android:path="/get/" />
</intent-filter>

Now you can go to this app directly from browser. Add a landing page with simple script like

<script>
    if( navigator.userAgent.match(/android/i) ) {    
        // redirect to the Play store    
    } else if( navigator.userAgent.match(/iPhone/i) ) {    
        // redirect IOS store    
    }
</script>

Now you can redirect the user to app if it is installed or in you case now to another app then to the playstore.

<a href="http://yourapp.com/get/"/>

More details are here.

Upvotes: 0

Rakshit Nawani
Rakshit Nawani

Reputation: 2604

You can use below function to open an application from it's package name

    public void runApp(String appName) throws IllegalArgumentException {
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    for ( ResolveInfo info : getPackageManager().queryIntentActivities( mainIntent, 0) ) {
        if ( info.loadLabel(getPackageManager()).equals(appName) ) {
            Intent launchIntent = getPackageManager().getLaunchIntentForPackage(info.activityInfo.applicationInfo.packageName);
            startActivity(launchIntent);
            return;
        }
    }
    throw new IllegalArgumentException("Application not found!");
}

Upvotes: 1

Related Questions