下面列出了android.content.pm.PackageManager#getApplicationLabel() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* check the application process name if process name is not qualified, then we think it is a service process and we will not init SDK
* @param pID
* @return
*/
private String getAppName(int pID) {
String processName = null;
ActivityManager am = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = appContext.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
try {
if (info.pid == pID) {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
// Log.d("Process", "Id: "+ info.pid +" ProcessName: "+
// info.processName +" Label: "+c.toString());
// processName = c.toString();
processName = info.processName;
return processName;
}
} catch (Exception e) {
// Log.d("Process", "Error>> :"+ e.toString());
}
}
return processName;
}
@CalledByNative
public static String getPackageLabel() {
// Third-party code does disk read on the getApplicationInfo call. http://crbug.com/614343
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
try {
PackageManager packageManager =
ContextUtils.getApplicationContext().getPackageManager();
ApplicationInfo appInfo = packageManager.getApplicationInfo(
getPackageName(), PackageManager.GET_META_DATA);
CharSequence label = packageManager.getApplicationLabel(appInfo);
return label != null ? label.toString() : "";
} catch (NameNotFoundException e) {
return "";
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
/**
* check the application process name if process name is not qualified, then we think it is a service process and we will not init SDK
* @param pID
* @return
*/
private String getAppName(int pID) {
String processName = null;
ActivityManager am = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = appContext.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
try {
if (info.pid == pID) {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
// Log.d("Process", "Id: "+ info.pid +" ProcessName: "+
// info.processName +" Label: "+c.toString());
// processName = c.toString();
processName = info.processName;
return processName;
}
} catch (Exception e) {
// Log.d("Process", "Error>> :"+ e.toString());
}
}
return processName;
}
/**
* Gets the name of the application that has been defined in AndroidManifest.xml
*
* @throws android.content.pm.PackageManager.NameNotFoundException
**/
public static String getApplicationName(Context ctx) throws NameNotFoundException {
if (ctx == null) {
throw new NullPointerException("Context cannot be null");
}
final PackageManager packageMgr = ctx.getPackageManager();
ApplicationInfo appInfo = null;
try {
appInfo = packageMgr.getApplicationInfo(ctx.getPackageName(), PackageManager.SIGNATURE_MATCH);
} catch (final NameNotFoundException e) {
throw new NameNotFoundException(e.getMessage());
}
final String applicationName = (String) (appInfo != null ? packageMgr.getApplicationLabel(appInfo) : "UNKNOWN");
return applicationName;
}
@CalledByNative
public static String getPackageLabel(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA);
CharSequence label = packageManager.getApplicationLabel(appInfo);
return label != null ? label.toString() : "";
} catch (NameNotFoundException e) {
return "";
}
}
private String getPackageLabel(Service service, String packagename){
PackageManager packageManager = service.getPackageManager();
ApplicationInfo ai;
try {
ai = packageManager.getApplicationInfo( packagename, 0);
} catch (final NameNotFoundException e) {
ai = null;
}
return (String) (ai != null ? packageManager.getApplicationLabel(ai) : packagename);
}
public static String g()
{
String s;
try
{
PackageManager packagemanager = d.getPackageManager();
s = (String)packagemanager.getApplicationLabel(packagemanager.getApplicationInfo(d.getPackageName(), 0));
}
catch (android.content.pm.PackageManager.NameNotFoundException namenotfoundexception)
{
namenotfoundexception.printStackTrace();
return "";
}
return s;
}
public String getApplicationName() {
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
try {
packageManager = getApplicationContext().getPackageManager();
applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
applicationInfo = null;
}
String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
return applicationName;
}
/**
* Gets app name.
*
* @return the app name
*/
public final String getAppName() {
String result;
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai = null;
try {
ai = pm.getApplicationInfo(context.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
if (EasyDeviceInfo.debuggable) {
Log.d(EasyDeviceInfo.nameOfLib, NAME_NOT_FOUND_EXCEPTION, e);
}
}
result = ai != null ? (String) pm.getApplicationLabel(ai) : null;
return CheckValidityUtil.checkValidData(result);
}
ApkItem(PackageManager pm, PackageInfo info, String path) {
try {
icon = pm.getApplicationIcon(info.applicationInfo);
} catch (Exception e) {
icon = pm.getDefaultActivityIcon();
}
title = pm.getApplicationLabel(info.applicationInfo);
versionName = info.versionName;
versionCode = info.versionCode;
apkfile = path;
packageInfo = info;
}
public static String getAppName(Context context) {
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
if (!(context instanceof Application))
context = context.getApplicationContext();
try {
packageManager = context.getPackageManager();
applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
applicationInfo = null;
}
CharSequence charSequence = packageManager.getApplicationLabel(applicationInfo);
return charSequence == null ? "" : (String) charSequence;
}
public static String getApplicationName(Context context) {
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
try {
packageManager = context.getApplicationContext().getPackageManager();
applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
applicationInfo = null;
}
String applicationName =
(String) packageManager.getApplicationLabel(applicationInfo);
return applicationName;
}
public static CharSequence getPackageLabel(Context ctx, String packageName) {
PackageManager lPackageManager = ctx.getPackageManager();
ApplicationInfo lApplicationInfo = null;
try {
lApplicationInfo = lPackageManager.getApplicationInfo(packageName,
0);
} catch (final PackageManager.NameNotFoundException e) {
return packageName;
}
return lPackageManager.getApplicationLabel(lApplicationInfo);
}
public static String getApplicationName(Context context, String packageName) {
PackageManager packageManager;
ApplicationInfo applicationInfo;
try {
packageManager = context.getApplicationContext().getPackageManager();
applicationInfo = packageManager.getApplicationInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
String applicationName =
(String) packageManager.getApplicationLabel(applicationInfo);
return applicationName;
}
@CalledByNative
public static String getPackageLabel(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA);
CharSequence label = packageManager.getApplicationLabel(appInfo);
return label != null ? label.toString() : "";
} catch (NameNotFoundException e) {
return "";
}
}
public static MaterialAboutTitleItem createAppTitleItem(Context c) {
Context applicationContext = c.getApplicationContext();
PackageManager packageManager = applicationContext.getPackageManager();
ApplicationInfo applicationInfo = applicationContext.getApplicationInfo();
CharSequence appName = packageManager.getApplicationLabel(applicationInfo);
Drawable applicationIcon = packageManager.getApplicationIcon(applicationInfo);
return createAppTitleItem(appName == null ? "" : appName.toString(), applicationIcon);
}
/**
* 获取应用名
* @param context
* @return
*/
public static String getApplicationName(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = context.getApplicationInfo();
return (String) packageManager.getApplicationLabel(applicationInfo);
} catch (Exception e) {
}
return "";
}
private ApplicationViewModel buildViewModelFromPackageInfo(PackageInfo pi,
DatabaseManager databaseManager,
PackageManager packageManager) {
ApplicationViewModel vm = new ApplicationViewModel();
vm.versionName = pi.versionName;
vm.packageName = pi.packageName;
vm.versionCode = pi.versionCode;
vm.requestedPermissions = pi.requestedPermissions;
if (vm.versionName != null)
vm.report = databaseManager.getReportFor(vm.packageName, vm.versionName);
else {
vm.report = databaseManager.getReportFor(vm.packageName, vm.versionCode);
}
if (vm.report != null) {
vm.trackers = databaseManager.getTrackers(vm.report.trackers);
}
try {
vm.icon = packageManager.getApplicationIcon(vm.packageName);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
vm.label = packageManager.getApplicationLabel(pi.applicationInfo);
vm.installerPackageName = packageManager.getInstallerPackageName(vm.packageName);
vm.isVisible = true;
return vm;
}
public static String getAppNameFromPackage(Context context, String packageName, boolean returnNull) {
final PackageManager pm = context.getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(packageName, 0);
} catch(final PackageManager.NameNotFoundException e) {
ai = null;
}
if(returnNull) {
return ai == null ? null : pm.getApplicationLabel(ai).toString();
}
return (String) (ai != null ? pm.getApplicationLabel(ai) : packageName);
}
@Override
protected void onCreate(Bundle b) {
super.onCreate(b);
actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(0xff00BCD4));
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(Html
.fromHtml("<font color='#ffffff'> <b> Shortcut </b> </font>"));
actionBar.setDisplayHomeAsUpEnabled(true);
mContent = new LinearLayout(this);
mContent.setOrientation(LinearLayout.VERTICAL);
mContent.setBackgroundColor(Color.WHITE);
setContentView(mContent);
for (int i = 0; i < CAPACITY; i++) {
final int bb = i;
final ShortCut txt = new ShortCut(getBaseContext(), null);
final Handler h = new Handler();
View v = new View(ShortcutSettings.this);
LayoutParams vv = new LayoutParams(LayoutParams.MATCH_PARENT, 1);
v.setBackgroundColor(0x33000000);
v.setPadding(0, 5, 0, 5);
mContent.addView(v, vv);
String Sys = Settings.System.getString(getContentResolver(), "PiSC" + bb);
ApplicationInfo ai = null;
PackageManager pm;
pm = getPackageManager();
if (Sys != null) {
try {
ai = pm.getApplicationInfo(Sys, 0);
String App_Name = (String) pm.getApplicationLabel(ai);
Drawable Icon = pm.getApplicationIcon(ai);
txt.setName(App_Name);
txt.setImage(Icon);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
else if (Sys == null) {
}
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Please Wait..",
Toast.LENGTH_LONG).show();
PickerApp.setCurSor(bb);
startActivity(new Intent(ShortcutSettings.this,
PickerApp.class));
}
});
mContent.addView(txt);
}
}