• 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> 
  • 相关阅读:
    MySQL存储引擎--MyISAM与InnoDB区别
    HTTP Keep-Alive模式
    php通过curl下载远程图片实例
    使用PHP QR Code生成二维码
    PHP中输出文件,怎么区别什么时候该用readfile() , fread(), file_get_contents(), fgets()
    SSDB 一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis.
    html头文件设置常用之<meta>设置缓存
    redis使用watch完成秒杀抢购功能
    Linux信号(signal) 机制分析
    php信号处理
  • 原文地址:https://www.cnblogs.com/yaxiaoke/p/5640077.html
Copyright © 2020-2023  润新知