Android列出所有应用,点击可运行~
Posted 卜筮老人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android列出所有应用,点击可运行~相关的知识,希望对你有一定的参考价值。
这个东西就比较容易了。
MainActivity.class
import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { List<AppsItemInfo> list; private GridView gridview; private PackageManager pManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); gridview = (GridView) findViewById(R.id.gridview); pManager = MainActivity.this.getPackageManager(); List<PackageInfo> appList = getAllApps(MainActivity.this); list = new ArrayList<AppsItemInfo>(); for (int i = 0; i < appList.size(); i++) { PackageInfo pinfo = appList.get(i); AppsItemInfo shareItem = new AppsItemInfo(); shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo)); shareItem.setLabel(pManager.getApplicationLabel( pinfo.applicationInfo).toString()); shareItem.setPackageName(pinfo.applicationInfo.packageName); list.add(shareItem); } gridview.setAdapter(new baseAdapter()); gridview.setOnItemClickListener(new ClickListener()); } public static List<PackageInfo> getAllApps(Context context) { List<PackageInfo> apps = new ArrayList<PackageInfo>(); PackageManager pManager = context.getPackageManager(); List<PackageInfo> packlist = pManager.getInstalledPackages(0); for (int i = 0; i < packlist.size(); i++) { PackageInfo pak = (PackageInfo) packlist.get(i); if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) { apps.add(pak); } } return apps; } private class baseAdapter extends BaseAdapter { LayoutInflater inflater = LayoutInflater.from(MainActivity.this); @Override public int getCount() {return list.size();} @Override public Object getItem(int position) {return null;} @Override public long getItemId(int position) {return position;} @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.apps, null); holder = new ViewHolder(); holder.icon = (ImageView) convertView .findViewById(R.id.apps_image); holder.label = (TextView) convertView .findViewById(R.id.apps_textview); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.icon.setImageDrawable(list.get(position).getIcon()); holder.label.setText(list.get(position).getLabel().toString()); return convertView; } } private class ViewHolder{ private ImageView icon; private TextView label; } private class ClickListener implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { Intent intent = new Intent(); intent = MainActivity.this.getPackageManager().getLaunchIntentForPackage(list.get(arg2).getPackageName()); startActivity(intent); } } private class AppsItemInfo { private Drawable icon; private String label; private String packageName; public Drawable getIcon() { return icon; } public void setIcon(Drawable icon) { this.icon = icon; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public String getPackageName() { return packageName; } public void setPackageName(String packageName) { this.packageName = packageName; } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="95dp" android:gravity="center" android:horizontalSpacing="12dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:verticalSpacing="15dp" /> </LinearLayout>
apps.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <ImageView android:id="@+id/apps_image" android:layout_width="48dip" android:layout_height="48dip" android:icon="@mipmap/ic_launcher" /> <TextView android:id="@+id/apps_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:maxEms="5" android:text="good" /> </LinearLayout>
以上是关于Android列出所有应用,点击可运行~的主要内容,如果未能解决你的问题,请参考以下文章
Android 应用程序不会启动,也不会列出错误/警告。使用片段和问题似乎与 var args = ViewFragmentArgs