我试图让类似行动启动一个应用程序,但我打了一些颠簸的道路。我似乎无法得到安装的应用程序在一个片段填充。
I am trying to make an app similar to Action Launcher, but I am hitting some bumps in the road. I can't seem to get the installed apps to populate in a fragment.
它要么崩溃,或者只是不与应用程序(这些应用程序的视图)填充。目前,我有它设置为装载所有的应用程序到一个ArrayList /列表,已安装的应用程序90,这样,我不担心。它显示,我有困难的时候与实际的应用程序。我试图动态地做到这一点,它不工作,我希望它的方式。
It either crashes or it just doesn't populate with the apps (the view of the apps). I currently have it set up to load all apps into an ArrayList/List, that has 90 apps installed so that I'm not worried about. It's displaying the actual apps that I am having the hardest time with. I am trying to do this dynamically, and it's not working the way I want it to.
/*
* Assume that
* GridView gv;
* Config cnf = new Config(); //personal class
* PackageManager pm;
* pm = getPackageManager();
*
*/
public void SelectItem(int possition) {
switch(possition){
case 2:
gv = (GridView) findViewById(R.id.gridView1);
// ArrayList<Apps> apps = cnf.getApps();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> apps = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
GridLayout gl = new GridLayout(returnContext());
gl.setOrientation(GridLayout.VERTICAL);
for (ResolveInfo appInfo : apps) {
String label = (String) appInfo.loadLabel(pm);
TextView name2 = new TextView(returnContext());
name2.setText(label);
gl.addView(name2);
ImageView img = new ImageView(returnContext());
img.setImageDrawable(appInfo.loadIcon(pm));
gl.addView(img);
}
gv.addView(gl); // I am getting a null reference exception here
}
}
和基本的XML为上述code
And the Base XML for the above code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="40dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
<ImageView
android:id="@+id/appicon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="2dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="6dip"
android:layout_marginTop="2dip"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/apptitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="20dip" />
</GridView>
</RelativeLayout>
我假设我的问题带有明显的谎言(我不是设置一个值正常。),而不是对我更大的问题不知道我在做什么,我乱七八糟的东西了(我不和可能没有)
I am assuming that my issue lies with the obvious (I am not setting a value properly.) and not the bigger issue of I don't know what I am doing and I messed something up (which I don't and probably did)
我上传了我的源 GitHub上 :)
I uploaded my source to GitHub :)
干杯。
由于绝对没人用一个简单的建议,即使来了,我理解了它自己是一个好一点的程序员。
Since absolutely no one came even with a simple suggestion, I figured it out myself like a good little programmer.
此解决方案将列出所有已安装的应用程序,与包括所有的应用程序与 Intent.CATEGORY_LAUNCHER
,而不包括所有的系统应用程序。我承认它是缓慢填充但最终的解决方案是实际使用的数据库,以帮助援助加载所有的项目。
This solution will list out all of the installed apps, with the inclusion of all apps with the Intent.CATEGORY_LAUNCHER
, without including all of the system apps. I will admit it is slow to populate but the end solution is to actually use a database to help aid in loading all of the items.
既然我已经发布了
github上 我的这些在
MIT许可。
所以下面的溶液
Since I have released this solution on
github I have these licensed under the
MIT licence.
So the following is the solution.
public List<Apps> listAllApps() {
List<Apps> apps = new ArrayList<Apps>();
List<ApplicationInfo> appinfo = pm.getInstalledApplications(0);
Apps app;
i = 0;
List<String> appnames = appNames();
for (int j = 0; j < appinfo.size(); j++) {
if (Config.in_array(appnames, appinfo.get(j).packageName)) {
app = new Apps();
app.setPackageName(appinfo.get(j).packageName);
app.setTitle(appinfo.get(j).loadLabel(pm).toString());
apps.add(app);
if (appinfo.get(j).packageName.contains("gallery") ||
appinfo.get(j).loadLabel(pm).toString().contains("gallery")) {
if (appinfo.get(j).name.contains("gallery")) {
setGalleryPackage(i, appinfo.get(j).packageName);
} else
setGalleryPackage(i, appinfo.get(j).packageName);
i++;
}
}
}
return apps;
}
public List appNames() {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> packs = pm.queryIntentActivities(mainIntent, 0);
List<String> appNames = new ArrayList<String>(packs.size());
for (ResolveInfo ai : packs) {
appNames.add(ai.activityInfo.packageName.toString());
}
return appNames;
}
要实际添加的项目在屏幕上,我不得不把(片段)内以下 onCreateView
方法
To actually add the items to the screen I had to put the following inside the (fragment) onCreateView
method
List<Apps> loadedApps = listAllApps();
Collections.sort(loadedApps, new Comparator<Apps>() {
@Override
public int compare(Apps lhs, Apps rhs) {
return lhs.getTitle().compareTo(rhs.getTitle());
}
});
// Config.makeColdToast(loadedApps.size());
for (final Apps a : loadedApps) {
final TableRow tb = new TableRow(Config.context);
tb.setId(i + 1000);
Drawable ico = null;
try {
Intent in = pm.getLaunchIntentForPackage(a.getPackageName());
if (in != null) {
ico = pm.getActivityIcon(in);
}
} catch (NameNotFoundException e) {
}
ImageView ima = new ImageView(Config.context);
ima.setImageDrawable(ico);
tb.addView(ima, new TableRow.LayoutParams(Config.dpToPx(50), Config.dpToPx(50)));
TextView name = new TextView(Config.context);
name.setText(a.getTitle());
a.setID(i);
tb.setPadding(Config.dpToPx(25), Config.dpToPx(10), Config.dpToPx(15), Config.dpToPx(10));
tb.setBackgroundColor(Color.argb(125, 0, 0, 0));
tb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = Config.context.getPackageManager().getLaunchIntentForPackage(a.getPackageName());
if (intent != null) {
startActivity(intent);
}
tb.setBackgroundColor(Color.TRANSPARENT);
}
});
tb.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
Config.makeColdToast("Long click!");
return false;
}
});
tb.addView(name, new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
gv.addView(tb, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
/*
* This is the gray line...
*/
TableRow tl = new TableRow(Config.context);
tl.setBackgroundResource(R.layout.customborder);
gv.addView(tl);
i++;
}
最后被引用多次的应用程序类。
And finally the Apps class that is referenced several times.
public class Apps {
private String title;
private String packageName;
private String versionName;
private int versionCode;
private String description;
private ResolveInfo resolveinfo;
Intent intent;
private int id;
public String getTitle() {
return title;
}
public void setResolveInfo(ResolveInfo ri) {
this.resolveinfo = ri;
}
public ResolveInfo getResolveInfo() {
return this.resolveinfo;
}
public int getID() {
return id;
}
public void setID(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public int getVersionCode() {
return versionCode;
}
public void setVersionCode(int versionCode) {
this.versionCode = versionCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public class AppViewHolder {
public TextView mTitle;
public ImageView mIcon;
public void setTitle(String title) {
mTitle.setText(title);
}
public void setIcon(Drawable img) {
if (img != null) {
mIcon.setImageDrawable(img);
}
}
}
public Drawable mIcon;
public void setIcons(Drawable icon) {
mIcon = icon;
}
public Drawable getIcon() {
return mIcon;
}
}