从包名获得应用程序名称
我想从应用程序包名称中获取应用程序名称。 有人请告诉我如何得到这个。
public class AppInstalledListener extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if(action.equals("android.intent.action.PACKAGE_ADDED")){ Logger.debug("DATA:"+intent.getData().toString()); } if(action.equals("android.intent.action.PACKAGE_REMOVED")){ Logger.debug("DATA:"+intent.getData().toString()); } if(action.equals("android.intent.action.PACKAGE_REPLACED")){ Logger.debug("DATA:"+intent.getData().toString()); } } }
你可以使用PackageManager
类来获取ApplicationInfo
:
final PackageManager pm = getApplicationContext().getPackageManager(); ApplicationInfo ai; try { ai = pm.getApplicationInfo( this.getPackageName(), 0); } catch (final NameNotFoundException e) { ai = null; } final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
这将返回其清单的<application>
标记中定义的应用程序名称。
尝试这个
final String packageName = "my.application.package" PackageManager packageManager= getApplicationContext().getPackageManager(); String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
并用你的包全名replace
packageName
。
你可以使用mContext.getPackageName()
获取packageName ,其中mContext = yourActivityName.this
为Activity, getActivity()为fragment。
看来你能够收到新的包的事件后添加一个非常简单的概念,以获得有关该包的所有相关信息,如一个这样的信息是应用程序的名称,所以这里是概念
– >您的设备包pipe理器拥有与之相关的所有信息,因此只需提供所有与包名相关的信息即可。
– >你还应该记住,意图给你“package:real_package_name”,所以首先你必须先通过溢出(我使用)或通过任何其他简单的string实现
– >这里的代码,希望你会得到你想要的,我也提供有关如何获得应用程序名称,应用程序图标,应用程序版本,应用程序版本代码等信息…..
public class NewAppReciver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")){ String[] a=intent.getData().toString().split(":"); String packageName=a[a.length-1]; List<PackageInfo> packageInfoList = context.getPackageManager().getInstalledPackages(0); for (int i = 0; i < packageInfoList.size(); i++) { PackageInfo packageInfo = packageInfoList.get(i); if(packageInfo.packageName.equals(packageName)){ String appName = packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString(); String appVersion = packageInfo.versionName; int appVerCode = packageInfo.versionCode; Drawable app_icon = packageInfo.applicationInfo.loadIcon(context.getPackageManager()); } } } } }
但是在卸载应用程序的时候,你只能得到软件包的名字,就像Un安装一样,所有其他的信息都被系统删除了。
public static String getAppNameFromPkgName(Context context, String Packagename) { try { PackageManager packageManager = context.getPackageManager(); ApplicationInfo info = packageManager.getApplicationInfo(Packagename, PackageManager.GET_META_DATA); String appName = (String) packageManager.getApplicationLabel(info); return appName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return ""; } }
PackageManager pm = getPackageManager(); IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.MAIN"); filter.addCategory("android.intent.category.HOME"); filter.addCategory("android.intent.category.DEFAULT"); Context context = getApplicationContext(); ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName()); ComponentName[] components = new ComponentName[] {new ComponentName("com.android.launcher", "com.android.launcher.Launcher"), component}; pm.clearPackagePreferredActivities("com.android.launcher"); pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);