我想从应用程序包名称中获取应用程序名称。有人请告诉我如何才能得到这个。
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());
}
}
}
答案 0 :(得分:185)
您可以使用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>
标记中定义的应用程序名称。
答案 1 :(得分:32)
试试这个
final String packageName = "my.application.package"
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
并将
packageName
替换为您的包裹全名。
您可以使用mContext.getPackageName()
获取 packageName ,其中mContext = yourActivityName.this
表示活动, getActivity()表示片段。
答案 2 :(得分:6)
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 "";
}
}
答案 3 :(得分:3)
似乎你能够收到添加新包的事件,之后它是一个非常简单的概念来获取有关该包的所有相关信息,就像一个这样的信息是应用程序名称所以这里是概念
- &GT;您的设备包管理器具有与之相关的所有信息,因此只需创建一个对象,它将为您提供与包名相关的所有信息。
- &GT;您还应该记住,intent会为您提供“package:real_package_name”,因此首先您必须先通过spilling(我使用过)或任何其他简单的String实现来获取真实姓名
- &GT;以下代码希望您能得到您想要的内容我还会提供有关如何获取应用名称,应用图标,应用版本,应用版本代码等的信息.....
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安装中,系统将删除所有其他信息。
答案 4 :(得分:-2)
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);