Android 开发之获取手机中所有App信息
程序员文章站
2022-07-05 22:19:42
效果图代码添加依赖implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'implementation 'androidx.recyclerview:recyclerview:1.1.0'AppInfo.javaimport android.graphics.drawable.Drawable;/** * created on 2020/8/3 20:22 * * @author Sca...
效果图
代码
- 添加依赖
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
- AppInfo.java
import android.graphics.drawable.Drawable; /**
* created on 2020/8/3 20:22
*
* @author Scarf Gong
*/ public class AppInfo { private Drawable image; private String appName; private String packageName; public AppInfo() { } public Drawable getImage() { return image; } public void setImage(Drawable image) { this.image = image; } public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } public String getPackageName() { return packageName; } public void setPackageName(String packageName) { this.packageName = packageName; } }
- AppUtils.java
/**
* created on 2020/8/3 20:24
*
* @author Scarf Gong
*/ public class AppUtils { private static final String TAG = "AppUtils"; public static List<AppInfo> scanLocalInstallAppList(PackageManager packageManager) { List myAppInfos = new ArrayList(); try { List packageInfos = packageManager.getInstalledPackages(0); for (int i = 0; i < packageInfos.size(); i++) { PackageInfo packageInfo = (PackageInfo) packageInfos.get(i); //过滤掉系统app if ((ApplicationInfo.FLAG_SYSTEM & packageInfo.applicationInfo.flags) != 0) { continue; } AppInfo myAppInfo = new AppInfo(); String appName = packageInfo.applicationInfo.loadLabel(packageManager).toString(); myAppInfo.setAppName(appName); myAppInfo.setPackageName(packageInfo.packageName); if (packageInfo.applicationInfo.loadIcon(packageManager) == null) { continue; } myAppInfo.setImage(packageInfo.applicationInfo.loadIcon(packageManager)); myAppInfos.add(myAppInfo); } } catch (Exception e) { Log.e(TAG, "获取应用包信息失败"); } return myAppInfos; } }
- AppAdapter.java
/**
* created on 2020/8/3 20:26
*
* @author Scarf Gong
*/ public class AppAdapter extends BaseQuickAdapter<AppInfo, BaseViewHolder> { public AppAdapter(int layoutResId, @Nullable List<AppInfo> data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder helper, AppInfo item) { helper.setImageDrawable(R.id.iv_img,item.getImage()); helper.setText(R.id.tv_name,item.getAppName()); helper.addOnClickListener(R.id.ll_layout); } }
- MainActivity.java
public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private AppAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initAdapter(); } private void initView() { mRecyclerView = findViewById(R.id.recyclerView); } private void initAdapter() { final List<AppInfo> appInfos = AppUtils.scanLocalInstallAppList(MainActivity.this.getPackageManager()); mAdapter = new AppAdapter(R.layout.item_app_info,appInfos); mAdapter.openLoadAnimation(); mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() { @Override public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { String packageName = appInfos.get(position).getPackageName(); openApp(packageName); } }); mRecyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3)); mRecyclerView.setAdapter(mAdapter); } private void openApp(String packageName) { Intent intent = getPackageManager().getLaunchIntentForPackage(packageName); startActivity(intent); } }
布局文件
- item_app_info.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/ll_layout" android:layout_width="120dp" android:layout_height="120dp" android:layout_gravity="center" android:gravity="center"> <ImageView android:id="@+id/iv_img" android:layout_width="90dp" android:layout_height="90dp" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:text="android" android:textColor="#000"/> </LinearLayout>
本文地址:https://blog.csdn.net/duoduo_11011/article/details/107773585