Android 获取系统的应用

来源:互联网 发布:卓讯海关数据怎么样 编辑:程序博客网 时间:2024/05/20 08:26

android 获取系统应用

效果:

这里写图片描述

布局文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_margin="10dp">    <LinearLayout        android:id="@+id/topLin"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_margin="10dp"        >        <RadioGroup            android:id="@+id/radioGroup"            android:layout_width="0dp"            android:layout_weight="4"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:weightSum="3.5"            >            <RadioButton                android:id="@+id/radioButton1"                android:layout_width="0dp"                android:layout_weight="1"                android:layout_height="wrap_content"                android:text="所有应用"                android:textSize="18dp"                android:checked="true"                />            <RadioButton                android:id="@+id/radioButton2"                android:layout_width="0dp"                android:layout_weight="1"                android:textSize="18dp"                android:layout_height="wrap_content"                android:text="显示图标的"                />            <RadioButton                android:id="@+id/radioButton3"                android:layout_width="0dp"                android:layout_weight="1"                android:textSize="18dp"                android:layout_height="wrap_content"                android:text="用户安装应用"                />        </RadioGroup>        <TextView            android:id="@+id/appNums"            android:layout_width="0dp"            android:layout_weight="0.8"            android:layout_height="wrap_content"            android:text="@string/appNums"            android:layout_gravity="center_vertical"            android:textSize="22dp"            />    </LinearLayout>        <GridView            android:id="@+id/mngGridView"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:listSelector="#00000000"            android:numColumns="5"            android:horizontalSpacing="10dp"            android:verticalSpacing="10dp"            android:layout_below="@id/topLin"            android:scrollbars="none" /></RelativeLayout>

获取应用的工具类

    public  static List<AppMngInfo> getSystemAppInfo(Context context,boolean islauncherApp,boolean isSystemApp){        List<AppMngInfo> systemAppInfo=new ArrayList<AppMngInfo>();//盒子预装的应用        List<PackageInfo> appList =getAllApps(context,islauncherApp);        PackageManager pManager =context.getPackageManager();        for (int i = 0;appList!=null&& i <appList.size(); i++) {            AppMngInfo localApp  = new AppMngInfo();            PackageInfo packageInfo =appList.get(i);            String appName =pManager.getApplicationLabel(packageInfo.applicationInfo).toString();//应用名称            Drawable appIcon =pManager.getApplicationIcon(packageInfo.applicationInfo);//应用图标            String packageName =packageInfo.packageName;            localApp.name=appName;            localApp.icon =packageInfo.applicationInfo.sourceDir;//图标路径            localApp.drawable =appIcon;            localApp.pkgName =packageName;            if((packageInfo.applicationInfo.flags&packageInfo.applicationInfo.FLAG_SYSTEM)>0){                //系统重复应用                localApp.isSystemApp =true;            }            if(isSystemApp){                systemAppInfo.add(localApp);            }else{                if(!localApp.isSystemApp){                    systemAppInfo.add(localApp);                }            }        }        return systemAppInfo;    }    //获取应用的所有app    private  static  List<PackageInfo> getAllApps(Context context,boolean islauncherApp){        PackageManager packageManager =context.getPackageManager();        List<PackageInfo> apps=new ArrayList<PackageInfo>();        List<PackageInfo> packList=packageManager.getInstalledPackages(0);//获取安装的apk        for (int i = 0; packList!=null&&i <packList.size(); i++) {            PackageInfo packInfo=packList.get(i);            //除当前应用            if(!packInfo.packageName.equals(GlobalConsts.packageName)){                if(islauncherApp){                    apps.add(packInfo);                }else{                    if(isLauncherAPP(context,packInfo.packageName)){                        apps.add(packInfo);                    }                }            }        }        return apps;    }    //launcher页面显示图标否    private static  boolean isLauncherAPP(Context context,String pkgname){        PackageInfo pi=null;        try {            pi=context.getPackageManager().getPackageInfo(pkgname,0);        } catch (PackageManager.NameNotFoundException e) {            e.printStackTrace();        }        if(pi == null){            return false;        }        Intent resolveIntent =new Intent(Intent.ACTION_MAIN);        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);        resolveIntent.setPackage(pi.packageName);        List<ResolveInfo> apps=context.getPackageManager().queryIntentActivities(resolveIntent,0);        if(apps!=null && apps.iterator().hasNext()){            return true;        }else{            return false;        }    }

MainActivity.java

public class MainActivity extends AppCompatActivity implements  RadioGroup.OnCheckedChangeListener,AdapterView.OnItemClickListener{    @Bind(R.id.mngGridView)    GridView mngGridView;    @Bind(R.id.radioButton1)    RadioButton radioButton1;    @Bind(R.id.radioButton2)    RadioButton radioButton2;    @Bind(R.id.radioButton3)    RadioButton radioButton3;    @Bind(R.id.radioGroup)    RadioGroup radioGroup;    @Bind(R.id.appNums)    TextView appNums;    String data1,data2;    private MngAdapter mngAdapter;    private List<AppMngInfo> appMngInfoList = new ArrayList<AppMngInfo>();    PackageManager pManager ;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);        pManager=MainActivity.this.getPackageManager();        appMngInfoList =MngUtils.getSystemAppInfo(MainActivity.this, true, true);        mngAdapter = new MngAdapter(appMngInfoList, MainActivity.this);        if (mngAdapter != null)            mngGridView.setAdapter(mngAdapter);        data1=getResources().getString(R.string.appNums);        data2= String.format(data1,appMngInfoList.size());        appNums.setText(data2);        //添加事件        radioGroup.setOnCheckedChangeListener(this);        mngGridView.setOnItemClickListener(this);    }    @Override    protected void onDestroy() {        super.onDestroy();        ButterKnife.unbind(this);    }    @Override    public void onCheckedChanged(RadioGroup radioGroup, int i) {       int radioButtonId= radioGroup.getCheckedRadioButtonId();        if(appMngInfoList!=null&&appMngInfoList.size()>0){            appMngInfoList.clear();        }        appMngInfoList.clear();        switch (radioButtonId){            case R.id.radioButton1:                appMngInfoList= MngUtils.getSystemAppInfo(MainActivity.this, true, true);                break;            case R.id.radioButton2:                appMngInfoList= MngUtils.getSystemAppInfo(MainActivity.this, false, true);                break;            case R.id.radioButton3:                appMngInfoList= MngUtils.getSystemAppInfo(MainActivity.this, false, false);                break;        }        data2 = String.format(data1, appMngInfoList.size());        appNums.setText(data2);        mngAdapter.setLists(appMngInfoList);    }    @Override    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {        //打开应用        Intent intent = pManager.getLaunchIntentForPackage(appMngInfoList.get(i).pkgName);        if(intent!=null){            startActivity(intent);        }else{            //该应用没有main activity            Toast.makeText(MainActivity.this,"该应用不可打开",Toast.LENGTH_LONG).show();        }    }   }

以上。

0 0
原创粉丝点击