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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 05:23:00

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/13171970/how-do-i-get-canonical-names-of-packages-of-deafult-apps-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!