Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if my application is set default or not in android?

Tags:

android

I want to check if my application is set as the default application for the Intents I'm handling inside my App.

As an example ff more than one application supports to open a specified file format. I need to make my application as a default application from my code. How it possible to make my application a default (from the code)? Can anyone help me?

At least I would like to check this on startup of my app and redirect the user to fill in some information if my App is not set as the default on the device.

like image 506
SRS Avatar asked Jul 27 '10 04:07

SRS


2 Answers

As far as I know this is not possible. That dialogue is handled by the system - your app will only be launched if the user picks your app from the list.

Allowing such a behaviour would impact the user's ability to control their default applications, and from a technical point of view would mean a process and its memory would have to be allocated to every app in the list every time such a list appeared.

like image 186
adamnfish Avatar answered Sep 28 '22 01:09

adamnfish


You could use this method:

public static boolean isOurAppDefault(Context context) {
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
    ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(browserIntent,PackageManager.MATCH_DEFAULT_ONLY);
    String defaultBrowserPkg = null;
    if (resolveInfo != null) {
        if (resolveInfo.activityInfo != null) {
            defaultBrowserPkg = resolveInfo.activityInfo.packageName;
        }
    }
    return TextUtils.equals(context.getPackageName(), defaultBrowserPkg);
}

It's actual for some editor or browser. In other case use different Uri data for intent.

like image 26
Djek-Grif Avatar answered Sep 28 '22 02:09

Djek-Grif