Search code examples
androidpackagecanonical-name

how do i get canonical names of packages of deafult apps in Android


I need to know the canonical names of a few Default Android applications like Email, Contacts & others. I want to be able to start them using their canonical names.

Like

Intent i = new Intent("com.pacakge.canonicalname");

So any ideas where i could get a list of the canonical package names of default Android apps?

thanks


Solution

  • There's a method in the PackageManager class called currentToCanonicalPackageNames(String[] names), which I suspect could be used to do what you're asking. So something like:

    String[] names = {"Email","Contacts"};
    try{
      String[] canonicalNames = context.getPackageManager().currentToCanonicalPackageNames(names);
    
      //do whatever you want with canonicalNames, e.g. launch the email app,
      Intent email = context.getPackageManager().getLaunchIntentForPackage(canonicalNames[0]);
      context.startActivity(email);
    
    }catch(NameNotFoundException e){
      Log.d("MY ACTIVITY NAME", "Package name(s) not found.");
    }
    

    I'm looking into doing this sort of thing myself, but haven't tried this code, so use with caution.

    EDIT: So tried it this evening, the solution may not be what you're looking for. Assuming you're starting with only the display names of these apps (technically referred to as application labels), you may have to actually go through all of the apps installed on the device looking for that application label. I just posted my solution to my blog, so take a look.