zoukankan      html  css  js  c++  java
  • 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> 
  • 相关阅读:
    JavaScript Location对象
    JavaScript History 对象
    JavaScript Navigator 对象
    C#实现windows系统重启、关机
    JavaScript ObjectURL | URL.createObjectURL和URL.revokeObjectURL
    依赖注入在 dotnet core 中实现与使用:4. 集成 Autofac
    Raid5的搭建、测试、取消
    Linux磁盘阵列(RAID)概述与实战
    Zookeeper超级用户使用案例:How to remove ACL protected ZK Node
    Kafka 权限管理实战(最全整理)
  • 原文地址:https://www.cnblogs.com/yaxiaoke/p/5640077.html
Copyright © 2011-2022 走看看