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

news/2024/11/16 19:51:51/

左一效果图。左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;@Overrideprotected 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@Overridepublic ViewHo onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.mlistview_layout, parent, false);return new ViewHo(inflate);}@Overridepublic 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());}@Overridepublic 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"><TextViewandroid: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.RecyclerViewandroid: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"><ImageViewandroid:id="@+id/img"android:layout_width="80dp"android:background="#fff"android:layout_height="80dp" /><TextViewandroid: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" /><ProgressBarandroid: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"/><TextViewandroid: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" /><applicationandroid: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>


http://www.ppmy.cn/news/720604.html

相关文章

大神救救我 手机通讯录 输3之后输个名字就运行不了了

#include<stdio.h> #include<stdlib.h> #include<windows.h> #include<assert.h>//用来解决程序终止问题&#xff0c;查阅网上资料得到 #define M 100 #define N1 20 #define N2 30 #define N3 30 char title[100]; void OPEN();//总运行 void menu1()…

安卓手机搭建一个服务器:按名字查询成绩

前提说明 使用PC机搭建一个服务器比较方便。其实手机端的文件互传更为普遍&#xff0c;最早的快牙&#xff0c;到现在的QQ面对面、小米快传、屏幕投射&#xff0c;都能通过“同一个WiFi”&#xff0c;又快又好地干许多意想不到的事。而将这一特性用于单独的移动端服务器还不普…

获取Android手机的手机名称

获取手机名称 BluetoothAdapter myDevice BluetoothAdapter.getDefaultAdapter();String deviceName myDevice.getName(); 上面的代码需要android.permission.BLUETOOTH权限

Android基础篇 查询当前运行的APP包名以及查询手机APP包名

一、包名概念 以及 签名的作用 包名&#xff08;Package name&#xff09;在Android系统中是判断一个App的唯一标识&#xff0c;不同的App可以有同样的名字&#xff0c;但是它的包名不可以相同 例如我可以自己编译生成一个App也叫“微信”&#xff0c;但是我App的包名不能为com…

请输入你的名字

package day03;public class Day03_Test01_Input02 {public static void main(String[] args) {// 1.生成input 输入方法 ; 表示语句结束 一定要写java.util.Scanner input new java.util.Scanner(System.in);System.out.println("请输入你的姓名&#xff1a;");//…

Java随机生成名字大全 手机号

有些项目要求实现随机生成人名&#xff0c;这里我用java实现&#xff0c;姓与名分开&#xff0c;最后组合在一起&#xff0c;话不多说&#xff0c;直接看代码。&#xff08;此源码借鉴与网上多个博客资源和自己的一点想法&#xff09; 第一种 public class Mytest1 { public …

为什么要把手机的名字取得很复杂?

基于合理的推测&#xff0c;凡是把水搅浑的人就是想混水摸鱼。所以凡是把名字搞得很复杂的&#xff0c;就是有不可告人的目的。凡是把名字取得简洁的&#xff0c;就是光明磊落的。那么怎样取名才能让消费者明明白白呢&#xff1f;最能表现出阶梯和层次的莫过于用阿拉伯数字&…

请输入名字

package day03;public class Day03_Test01_Input {// 程序的入口函数public static void main(String[] args) {//这里变量取什么名&#xff0c;下面就用什么.//例如&#xff1a;这里取名input&#xff0c;下面就用input.// 键盘上输入的字符 使用input进行接收java.util.Scann…