获取手机上其他应用的图标,名字。用RecyclerView整合

左一效果图。左2MainActivity

---------------------------------//app信息工具类---------------------------------------

import android.graphics.drawable.Drawable;
import java.io.Serializable;
public class AppInfo implements Serializable {
    /** 序列号 */
    private static final long serialVersionUID = -6660233212727684115L;
    /** 名称 */
    public String name;
    /** (路径) */
    public String path;
    /** 图标 */
    public Drawable icon;
    /** 包名 */
    public String packageName;

    public int a;

    public int b;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Drawable getIcon() {
        return icon;
    }

    public void setIcon(Drawable icon) {
        this.icon = icon;
    }

    public String getPackageName() { return packageName; }
    
    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

public  int setProportion(int c){ return  this.a=c;}

public  int getx(){ return   a;}

public int setliuliang(int d){ return  this.b=d; }

public String gety(){ return String.valueOf(b);}
}

----------------------------------ReclyerView代码---------------------------------------

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RecyclerActivity extends AppCompatActivity {
    private List<PackageInfo> packageInfoList;
    private List<AppInfo> applicationInfoList;
    private RecyclerView mlistview;
    private RecyclerAdapter recyclerAdapter;
    private PackageManager pm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        mlistview = findViewById(R.id.mlistview);
        getAllAppNames();
        recyclerAdapter = new RecyclerAdapter(applicationInfoList);
        mlistview.setAdapter(recyclerAdapter);
        recyclerAdapter.notifyDataSetChanged();
        mlistview.setLayoutManager(new GridLayoutManager(this, 1));
    }
    public void getAllAppNames() {
        pm = getPackageManager();
        // 获取到所有安装了的应用程序的信息,包括那些卸载了的,但没有清除数据的应用程序
        packageInfoList=pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
        applicationInfoList = new ArrayList<>();
        List<ApplicationInfo> list = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
        Collections.sort(list, new ApplicationInfo.DisplayNameComparator(pm));// 排序

        applicationInfoList.clear();
        for (int i = 0; i < list.size(); i++) {
            //非系统程序
            int x=(int)(Math.random()*100);
            int y=(int)(Math.random()*100);
            if ((list.get(i).flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                applicationInfoList.add(getAppInfo(list.get(i),x,y));//如果非系统应用,则添加至appList
            }  //系统程序
        }
    }
    private AppInfo getAppInfo(ApplicationInfo app,int a,int b) {
        AppInfo appInfo = new AppInfo();
        appInfo.setName(app.loadLabel(pm).toString());
        appInfo.setIcon(app.loadIcon(pm));
        appInfo.setPackageName(app.packageName);
        appInfo.setProportion(a);
        appInfo.setliuliang(b);
        return appInfo;
    }
    }

-----------------------------ReclyerAdapter代码-------------------------------------

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHo> {
    private  final  List<AppInfo> list;
    public RecyclerAdapter(List<AppInfo> list) {
        this.list = list;
    }
    @NonNull
    @Override
    public ViewHo onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.mlistview_layout, parent, false);
        return new ViewHo(inflate);
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerAdapter.ViewHo holder, int position) {
        holder.img.setImageDrawable(list.get(position).getIcon());
        holder.tv_appname.setText(list.get(position).getName());
        holder.progressBar.setProgress(list.get(position).getx());
        holder.tv.setText(list.get(position).gety());
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    
    public static class ViewHo extends RecyclerView.ViewHolder {
        ImageView img;
        TextView tv_appname;
        ProgressBar progressBar;
        TextView tv;
        public ViewHo(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            tv_appname = itemView.findViewById(R.id.tv_appname);
            progressBar=itemView.findViewById(R.id.progressBar);
            tv=itemView.findViewById(R.id.tv);
        }
    }
}

-------------------------------------activity_main--------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

-----------------------------activity_main3-------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/mlistview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

-------------------------------------mlistviw_layout---------------------------

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/img"
        android:layout_width="80dp"
        android:background="#fff"
        android:layout_height="80dp" />
        <TextView
        android:id="@+id/tv_appname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textColor="#000"
        android:layout_marginLeft="80dp"
        android:textSize="20dp" />
            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="120dp"
                android:layout_height="20dp"
                style="?android:attr/progressBarStyleHorizontal"
                android:max="100"
                android:layout_marginLeft="80dp"
                android:layout_marginTop="30dp"
                android:progress="0"/>
            <TextView
                android:id="@+id/tv"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:textColor="#FFF79E38"
                android:text="@string/_222222"
                android:textSize="20sp"
                android:layout_marginLeft="200dp"
                android:layout_marginTop="30dp"
                />
</RelativeLayout>

------------------------------------manifest.xml-----------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bitmap">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.BitMap">
        <activity android:name=".RecyclerActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值