请告诉我如何使用应用程序名称而不是包名来显示安装应用程序的listview?我的应用程序使用package namelike(“com.mypackage”)显示选择安装应用程序我想使用应用程序名称显示应用程序列表(“myapplication”如何更改此代码请帮助我
package com.gfsmart.filemanager;
import java.util.Map;
//import com.gfsmart.filemanager.provider.MyProvider;
import android.app.ListActivity;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity implements OnItemClickListener {
/* whether or not to include system apps */
private static final boolean INCLUDE_SYSTEM_APPS = false;
private ListView mAppsList;
private AppListAdapter mAdapter;
private List<App> mApps;
String APKFilePath = "mnt/sdcard/foldername/";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppsList = (ListView) findViewById(R.id.appslist);
mAppsList.setOnItemClickListener(this);
mApps = loadInstalledApps(INCLUDE_SYSTEM_APPS);
mAdapter = new AppListAdapter(getApplicationContext());
mAdapter.setListItems(mApps);
mAppsList.setAdapter(mAdapter);
new LoadIconsTask().execute(mApps.toArray(new App[]{}));
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long
id) {
final App app = (App) parent.getItemAtPosition(position);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String msg = app.getTitle() + "\n\n" +
"Version " + app.getVersionName() + " (" +
app.getVersionCode() + ")" +
(app.getDescription() != null ? ("\n\n" + app.getDescription()) : "");
builder.setMessage(msg)
.setCancelable(true)
.setTitle(app.getTitle())
.setIcon(mAdapter.getIcons().get(app.getPackageName()))
.setPositiveButton("Launch", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// start the app by invoking its launch intent
Intent i =
getPackageManager().getLaunchIntentForPackage(app.getPackageName());
try {
if (i != null) {
startActivity(i);
} else {
i = new Intent(app.getPackageName());
startActivity(i);
}
} catch (ActivityNotFoundException err) {
Toast.makeText(MainActivity.this, "Error launching app",
Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private List<App> loadInstalledApps(boolean includeSysApps) {
List<App> apps = new ArrayList<App>();
PackageManager packageManager = getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
//PackageManager.GET_META_DATA
for(int i=0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ( ((p.applicationInfo).equals("com.gfsmart.filemanager")) ||
((p.packageName).equals("com.mypackage")) ||
((p.packageName).equals("com.doodlejoy.studio.kidsdoojoy")) ||
((p.packageName).equals("com.fullfat.android.agentdash"))) {
//
App app = new App();
app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
app.setPackageName(p.packageName);
app.setVersionName(p.versionName);
app.setVersionCode(p.versionCode);
CharSequence description =
p.applicationInfo.loadDescription(packageManager);
app.setDescription(description != null ? description.toString() :
"");
apps.add(app);
}
}
return apps;
}
/**
* An asynchronous task to load the icons of the installed applications.
*/
private class LoadIconsTask extends AsyncTask<App, Void, Void> {
@Override
protected Void doInBackground(App... apps) {
Map<String, Drawable> icons = new HashMap<String, Drawable>();
PackageManager manager =
getApplicationContext().getPackageManager();
for (App app : apps) {
String pkgName = app.getPackageName();
Drawable ico = null;
try {
Intent i = manager.getLaunchIntentForPackage(pkgName);
if (i != null) {
ico = manager.getActivityIcon(i);
}
} catch (NameNotFoundException e) {
Log.e("ERROR", "Unable to find icon for package '" +
pkgName + "': " + e.getMessage());
}
icons.put(app.getPackageName(), ico);
}
mAdapter.setIcons(icons);
return null;
}
@Override
protected void onPostExecute(Void result) {
mAdapter.notifyDataSetChanged();
}
}
}
答案 0 :(得分:1)
private void loadInstalledApps() {
PackageManager packageManager = getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo applicationInfo = p.applicationInfo;
String name = packageManager.getApplicationLabel(applicationInfo).toString();
Log.d("xxx", name);
}
}
我修改了代码,应用名称存储在变量name