Recyclew+Fragment获取手机所有程序

来源:互联网 发布:数据库系统的应用 编辑:程序博客网 时间:2024/06/05 19:05

这里写图片描述

下面实现效果,如上图所示
具体代码实现如下:
1、fragment_tab4.xml

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"><android.support.v7.widget.RecyclerView    android:id="@+id/rv_app_manager"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginTop="1dp"></android.support.v7.widget.RecyclerView><LinearLayout    android:id="@+id/ll_app_loading"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:visibility="invisible"    >    <ProgressBar        android:id="@+id/pd_app_manager"        android:layout_width="60dp"        android:layout_height="60dp" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="25sp"        android:text="正在加载应用程序"        /></LinearLayout></FrameLayout>

2、FourthTabFragment代码如下:

public class FourthTabFragment extends Fragment {    private View view;    private LinearLayout ll_app_loading;    private RecyclerView recyclerView;    private Context context;    private AppManagerAdapter adapter;    private List<AppInfo> userAppinfos = new ArrayList<>();//初始化数据    List<AppInfo> appinfos;    private AppInfoProvider provider;    private Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            appinfos = (List<AppInfo>) msg.obj;            ll_app_loading.setVisibility(View.INVISIBLE);            userAppinfos.addAll(appinfos);//            adapter.notifyDataSetChanged();        }    };    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        view = inflater.inflate(R.layout.fragment_tab4, null);        context = getActivity();//初始化context        ll_app_loading = (LinearLayout) view.findViewById(R.id.ll_app_loading);        ll_app_loading.setVisibility(View.VISIBLE);        recyclerView = (RecyclerView) view.findViewById(R.id.rv_app_manager);        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);        recyclerView.setLayoutManager(linearLayoutManager);        recyclerView.setHasFixedSize(true);        // userAppinfos = AppInfoProvider.getUserApps(appInfos);        adapter = new AppManagerAdapter(userAppinfos);        recyclerView.setAdapter(adapter);//在子线程之前就设置adapter        new Thread() {            @Override            public void run() {                super.run();                provider = new AppInfoProvider(context);                Message msg = new Message();                msg.obj = provider.getUserApps();                handler.sendMessage(msg);            }        }.start();        return view;    }}

3、获取手机应用程序AppInfoProvider

public class AppInfoProvider {    private static final String TAG = "AppInfoProvider";    private Context context;    private static PackageManager packageManager;    public AppInfoProvider(Context context) {        this.context = context;        packageManager = context.getPackageManager();    }    public static List<AppInfo> getAllApps() {       /* Drawable appIcon;         String appName;         String packName;*/        List<AppInfo> appInfos = new ArrayList<>();//用来存储获取的应用信息数据        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);        Log.i(TAG, "packageInfos: " + packageInfos);        for (PackageInfo info : packageInfos) {            AppInfo myApp = new AppInfo();            String packageName = info.packageName;//a            myApp.setPackName(packageName);            Log.i(TAG, "packageName: " + packageName);            ApplicationInfo appinfo = info.applicationInfo;            Drawable icon = appinfo.loadIcon(packageManager);            myApp.setAppIcon(icon);//a            String appname = appinfo.loadLabel(packageManager).toString();            myApp.setAppName(appname);            if (filterApp(appinfo)) {                myApp.setSystemApp(false);            } else {                myApp.setSystemApp(true);            }            appInfos.add(myApp);        }        return appInfos;    }    public static List<AppInfo> getUserApps() {        List<AppInfo> userAppInfos = new ArrayList<>();        for (AppInfo appinfo : getAllApps()) {            if (!appinfo.isSystemApp()) {                userAppInfos.add(appinfo);            }        }        return userAppInfos;    }    // 判断某个应用程序是不是三方的应用程序    public static boolean filterApp(ApplicationInfo info) {        if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {            return true;        } else if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {            return true;        }        return false;    }}

4、AppManagerAdapter的代码如下:

public class AppManagerAdapter extends RecyclerView.Adapter<AppManagerAdapter.ViewHolder> {    private static ImageView iv;    private static TextView tv;    private List<AppInfo> appInfos;    private static final String TAG = "AppManagerAdapter";    public AppManagerAdapter(List<AppInfo> appInfos) {        this.appInfos = appInfos;    }    @Override    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view = View.inflate(parent.getContext(), R.layout.app_manager_item, null);        ViewHolder viewHolder = new ViewHolder(view);        return viewHolder;    }    @Override    public void onBindViewHolder(ViewHolder holder, int position) {        AppInfo info = appInfos.get(position);        holder.iv_icon.setImageDrawable(info.getAppIcon());        holder.tv_app_name.setText(info.getAppName());    }    @Override    public int getItemCount() {        return appInfos.size();    }    public class ViewHolder extends RecyclerView.ViewHolder {        private ImageView iv_icon;        private TextView tv_app_name;        public ViewHolder(View view) {            super(view);            iv_icon = (ImageView) view.findViewById(R.id.iv_icon);            tv_app_name = (TextView) view.findViewById(R.id.tv_app_name);        }    }}

5、AppInfo数据如下

public class AppInfo {    private Drawable appIcon;    private String appName;    private String packName;    private boolean isSystemApp;    public Drawable getAppIcon() {        return appIcon;    }    public void setAppIcon(Drawable appIcon) {        this.appIcon = appIcon;    }    public String getAppName() {        return appName;    }    public void setAppName(String appName) {        this.appName = appName;    }    public String getPackName() {        return packName;    }    public void setPackName(String packName) {        this.packName = packName;    }    public boolean isSystemApp() {        return isSystemApp;    }    public void setSystemApp(boolean systemApp) {        isSystemApp = systemApp;    }    @Override    public String toString() {        return "AppInfo{" +                "appIcon=" + appIcon +                ", appName='" + appName + '\'' +                ", packName='" + packName + '\'' +                ", isSystemApp=" + isSystemApp +                '}';    }}
0 0