大家好,你能帮我在主屏幕上显示所有已安装的应用吗?你能给我一些简单网格视图的源代码吗?
答案 0 :(得分:3)
看看这段代码。
package com.example.android.apis.view;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.List;
import com.example.android.apis.R;
public class Grid1 extends Activity {
GridView mGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadApps(); // do this in onresume?
setContentView(R.layout.grid_1);
mGrid = (GridView) findViewById(R.id.myGrid);
mGrid.setAdapter(new AppsAdapter());
}
private List<ResolveInfo> mApps;
private void loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
}
public class AppsAdapter extends BaseAdapter {
public AppsAdapter() {
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i;
if (convertView == null) {
i = new ImageView(Grid1.this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new GridView.LayoutParams(50, 50));
} else {
i = (ImageView) convertView;
}
ResolveInfo info = mApps.get(position);
i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
return i;
}
public final int getCount() {
return mApps.size();
}
public final Object getItem(int position) {
return mApps.get(position);
}
public final long getItemId(int position) {
return position;
}
}
}
源API演示..
答案 1 :(得分:1)
以下是获取当前已安装软件包的方法。
PackageManager packageManager = null;
List<ApplicationInfo> appsList = new ArrayList<ApplicationInfo>();
try {
packageManager = getPackageManager();
if (packageManager != null) {
appsList = packageManager.getInstalledApplications(0);
}
} catch (Exception e1) {
Log.v("TAG", "This is my error: ", e1);
}
然后,您可以从ApplicationInfo对象中检索各种信息(包括图标,名称等)。