我的列表视图显示用户安装的应用程序的列表,但是如何获取被单击项的软件包名称?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView userInstalledApps = (ListView)findViewById(R.id.installed_app_list);
List<AppList> installedApps = getInstalledApps();
AppAdapter installedAppAdapter = new AppAdapter(MainActivity.this, installedApps);
userInstalledApps.setAdapter(installedAppAdapter);
}
private List<AppList> getInstalledApps() {
List<AppList> res = new ArrayList<AppList>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((isSystemPackage(p) == false)) {
String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(new AppList(appName, icon));
}
}
return res;
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false;
}
这是我尝试过的方法,但是如何获得软件包名称?
userInstalledApps.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:0)
您将必须更改AppList
对象以包括包名称,并在填充名称时将其填充。然后,当用户从列表中选择项目时,您可以从适配器获取所选项目的AppList
,并从中获取包装名称。
基本示例;
public class AppList {
private String name;
Drawable icon;
String packageName;
public AppList(String name, Drawable icon, String packageName) {
this.name = name;
this.icon = icon;
this.packageName = packageName
}
.....
答案 1 :(得分:0)
请在applist.java文件中更改您的代码段
private String name;
String package_name;
Drawable icon;
public AppList(String name, String package_name, Drawable icon) {
this.name = name;
this.icon = icon;
this.package_name = package_name;
}
public String getName() {
return name;
}
public Drawable getIcon() {
return icon;
}
public String getPackage_name() {
return package_name;
}
还更改列表Java文件代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView userInstalledApps = (ListView) findViewById(R.id.installed_app_list);
final List<AppList> installedApps = getInstalledApps();
AppAdapter installedAppAdapter = new AppAdapter(MainActivity.this, installedApps);
userInstalledApps.setAdapter(installedAppAdapter);
userInstalledApps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
AppList appList = installedApps.get(i);
Log.e("pacakge name", appList.getPackage_name());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private List<AppList> getInstalledApps() {
List<AppList> res = new ArrayList<AppList>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((isSystemPackage(p) == false)) {
String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
String package_name = p.applicationInfo.packageName;
res.add(new AppList(appName, package_name, icon));
}
}
return res;
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false;
}