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
- Android 获取系统的应用
- android 获取系统应用
- 获取android系统应用
- android 获取系统已经安装的应用
- android获取系统应用大小的方法
- 如何获取Android系统应用的Action
- Android 获取系统内的应用信息
- Android获取系统应用及安装应用的权限列表
- Android获取系统应用及安装应用的权限列表
- 获取Android系统应用的包名以及应用名!
- Android 获取系统应用信息
- Android 获取系统应用信息
- Android应用获取系统属性
- android 应用获取系统权限
- android 应用获取系统权限
- android 获取系统安装的所有的浏览器应用
- Android 获取常用的系统及应用的版本信息
- android 获取系统内所有应用的信息
- 将导入的图片变成背景透明的材质球
- sql中使用存储过程得到随机数
- Unity与c++ 结构体中带数组参数传递
- maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea
- APP 微信支付java后台代码(解决支付失败返回-1)
- Android 获取系统的应用
- HDU - 4548.美素数
- 小白刷Pat -1008
- 代码图形分析工具Understand
- Qt 让弹出的窗口居中显示
- httpd-2.4.25-x86.zip
- 简单的WebView自适应并嵌套在ScrollView里
- UML下的九种图~用例图
- adb命令常用命令