获取安装的应用
程序员文章站
2022-05-28 22:40:24
...
在main.xml 中:
<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:numColumns="auto_fit" android:verticalSpacing="15dp" android:horizontalSpacing="12dp" android:columnWidth="95dp" android:stretchMode="columnWidth" android:gravity="center" />
然后在item.xml给item布局
<?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="@drawable/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>
在Activity中显示
public class UnloadingActivity extends Activity { // 用来记录应用程序的信息 List<AppsItemInfo> list; private GridView gridview; private PackageManager pManager; private String uninstallPackage; // int state; // int mNum; // final static int PROGRESS_DIALOG = 0; // ProgressThread progressThread; // ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.main); // 取得gridview gridview = (GridView) findViewById(R.id.gridview); // 获取图片、应用名、包名 pManager = UnloadingActivity.this.getPackageManager(); List<PackageInfo> appList = getAllApps(UnloadingActivity.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的Adapter 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()里的值如果<=0则为自己装的程序,否则为系统工程自带 if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) { // 添加自己已经安装的应用程序 apps.add(pak); } } return apps; } private class baseAdapter extends BaseAdapter { LayoutInflater inflater = LayoutInflater.from(UnloadingActivity.this); @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } public final class ViewHolder { private ImageView icon; private TextView label; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if (convertView == null) { // 使用View的对象itemView与R.layout.item关联 convertView = inflater.inflate(R.layout.item, 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 ClickListener implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { AppsItemInfo app = list.get(position); uninstallPackage = app.packageName; AppUtils.uninstallApk(UnloadingActivity.this, uninstallPackage); } } // 自定义一个 AppsItemInfo 类,用来存储应用程序的相关信息 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; } } }
package com.flyou.utils; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; /** * 跟App相关的辅助类 * * * */ public class AppUtils { private AppUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * 获取应用程序名称 */ public static String getAppName(Context context) { try { PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0); int labelRes = packageInfo.applicationInfo.labelRes; return context.getResources().getString(labelRes); } catch (NameNotFoundException e) { e.printStackTrace(); } return null; } /** * [获取应用程序版本名称信息] * * @param context * @return 当前应用的版本名称 */ public static String getVersionName(Context context) { try { PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0); return packageInfo.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); } return null; } }
public static void uninstallApk(Context ctx, String packageName) { Uri packageURI = Uri.parse("package:" + packageName); Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI); ctx.startActivity(uninstallIntent); }
上一篇: TextView展开收起,资料收集
下一篇: 在jupyter notebook导入keras出错:ModuleNotFoundError: No module named 'keras' 解决办法
推荐阅读