从手机中扫描以com keyboard themestudio为前缀的apk包,使用列表的形式展现

从手机中扫描以com keyboard themestudio为前缀的apk包,使用列表的形式展现,第1张

概述apk 包可以使用PackageManager获取,apk 包中的资源可以获取其对应的Context,再通过Context获取对应的Resouce获取提示2: apk 是指已经安装过的程序,不是存在sdcard. 上的apk安装包. 先上效果图       开始代码部分 首先是布局文件activity_main.xml <?xml version="1.0" encoding="utf-8"?> @H_502_2@

apk 包可以使用PackageManager获取,apk 包中的资源可以获取其对应的Context,再通过Context获取对应的Resouce获取提示2: apk 是指已经安装过的程序,不是存在sdcard. 上的apk安装包.

先上效果图

 

 

 

开始代码部分

首先是布局文件activity_main.xml

<?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"    androID:orIEntation="vertical"    tools:context="com.nf.appList.MainActivity">    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="48dp"        androID:paddingleft="16dp"        androID:paddingRight="16dp">        <Switch            androID:ID="@+ID/switch_type"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:checked="true" />        <EditText            androID:ID="@+ID/edit_input"            androID:layout_wIDth="0dp"            androID:layout_height="match_parent"            androID:layout_weight="1"            androID:hint="请输入包名前缀"            androID:singleline="true"            androID:textcolor="#333333"            androID:textSize="14sp" />        <button            androID:ID="@+ID/btn_scan"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="扫描"            androID:textcolor="#333333"            androID:textSize="14sp" />    </linearLayout>    <relativeLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent">        <ListVIEw            androID:ID="@+ID/List_vIEw"            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent" />        <TextVIEw            androID:ID="@+ID/tv_empty"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_centerInParent="true"            androID:text="没有找到任何包"            androID:textcolor="#333333"            androID:textSize="14sp"            androID:visibility="gone" />    </relativeLayout></linearLayout>

ListVIEw的item布局

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="58dp"    androID:gravity="center_vertical"    androID:paddingleft="16dp"    androID:paddingRight="16dp">    <!-- tools 域 表示 仅在预览时生效 -->    <ImageVIEw        androID:ID="@+ID/iv_app_icon"        androID:layout_wIDth="40dp"        androID:layout_height="40dp"        tools:src="@mipmap/ic_launcher" />    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:layout_marginleft="16dp"        androID:orIEntation="vertical">        <TextVIEw            androID:ID="@+ID/tv_app_name"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:textcolor="#333333"            androID:textSize="14sp"            tools:text="应用名称" />        <TextVIEw            androID:ID="@+ID/tv_app_package"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_margintop="2dp"            androID:textcolor="#888888"            androID:textSize="12sp"            tools:text="应用包名" />    </linearLayout></linearLayout>

然后是实体类SimpleAppInfo.class

public class SimpleAppInfo {    private String appname;    private String appPackage;    private Drawable appIcon;    private String valuesAppname;    private Drawable drawablePreIEwimg;    private boolean system;    private String packagePath;    private String versionname;    private int versionCode;    public SimpleAppInfo(String appname,String appPackage,Drawable appIcon,boolean system,String packagePath,String versionname,int versionCode) {        this.appname = appname;        this.appPackage = appPackage;        this.appIcon = appIcon;        this.system = system;        this.packagePath = packagePath;        this.versionname = versionname;        this.versionCode = versionCode;    }    public String getPackagePath() {        return packagePath;    }    public voID setPackagePath(String packagePath) {        this.packagePath = packagePath;    }    public String getVersionname() {        return versionname;    }    public voID setVersionname(String versionname) {        this.versionname = versionname;    }    public int getVersionCode() {        return versionCode;    }    public voID setVersionCode(int versionCode) {        this.versionCode = versionCode;    }    public boolean isSystem() {        return system;    }    public voID setSystem(boolean system) {        this.system = system;    }    public String getAppname() {        return appname;    }    public voID setAppname(String appname) {        this.appname = appname;    }    public String getAppPackage() {        return appPackage;    }    public voID setAppPackage(String appPackage) {        this.appPackage = appPackage;    }    public Drawable getAppIcon() {        return appIcon;    }    public voID setAppIcon(Drawable appIcon) {        this.appIcon = appIcon;    }    public String getValuesAppname() {        return valuesAppname;    }    public voID setValuesAppname(String valuesAppname) {        this.valuesAppname = valuesAppname;    }    public Drawable getDrawablePreIEwimg() {        return drawablePreIEwimg;    }    public voID setDrawablePreIEwimg(Drawable drawablePreIEwimg) {        this.drawablePreIEwimg = drawablePreIEwimg;    }}

接下来就是核心代码了MainActivity.class

public class MainActivity extends AppCompatActivity {    private TextVIEw tvEmpty;    private ListVIEw ListVIEw;    private button btnScan;    private EditText editinput;    private List<SimpleAppInfo> filterappList = new ArrayList<>();    private BaseAdapter applistadapter;    private Switch switchType;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        tvEmpty = findVIEwByID(R.ID.tv_empty);        ListVIEw = findVIEwByID(R.ID.List_vIEw);        btnScan = findVIEwByID(R.ID.btn_scan);        editinput = findVIEwByID(R.ID.edit_input);        switchType = findVIEwByID(R.ID.switch_type);        switchType.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() {            @OverrIDe            public voID onCheckedChanged(Compoundbutton buttonVIEw,boolean isChecked) {                applistadapter.notifyDataSetChanged();            }        });        btnScan.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                //防止重复点击                btnScan.setEnabled(false);                ListVIEw.setVisibility(VIEw.GONE);                tvEmpty.setVisibility(VIEw.VISIBLE);                tvEmpty.setText("正在扫描");                //开子线程做耗时的工作                new Thread() {                    @OverrIDe                    public voID run() {                        final List<SimpleAppInfo> appsInfo = getAppsInfo();                        //切换到主线程 更新数据                        runOnUiThread(new Runnable() {                            @OverrIDe                            public voID run() {                                String filterStr = editinput.getText().toString().trim();                                if (TextUtils.isEmpty(filterStr)) {                                    filterappList = appsInfo;                                } else {//过滤                                    filterappList.clear();                                    for (SimpleAppInfo appInfo : appsInfo) {                                        if (appInfo.getAppPackage().startsWith(filterStr)) {                                            filterappList.add(appInfo);                                        }                                    }                                }                                if (filterappList.size() == 0) {                                    tvEmpty.setVisibility(VIEw.VISIBLE);                                    ListVIEw.setVisibility(VIEw.GONE);                                    tvEmpty.setText("没有任何数据");                                } else {                                    tvEmpty.setVisibility(VIEw.GONE);                                    ListVIEw.setVisibility(VIEw.VISIBLE);                                    applistadapter.notifyDataSetChanged();                                }                                //更新完成后  开放 按钮                                btnScan.setEnabled(true);                            }                        });                    }                }.start();            }        });        applistadapter = new BaseAdapter() {            @OverrIDe            public int getCount() {                return filterappList.size();            }            @OverrIDe            public Object getItem(int position) {                return filterappList.get(position);            }            @OverrIDe            public long getItemID(int position) {                return 0;            }            @OverrIDe            public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {                VIEwHolder holder;                if (convertVIEw == null) {                    convertVIEw = VIEw.inflate(MainActivity.this,R.layout.item,null);                    holder = new VIEwHolder(convertVIEw);                    convertVIEw.setTag(holder);                } else {                    holder = (VIEwHolder) convertVIEw.getTag();                }                SimpleAppInfo appInfo = filterappList.get(position);                holder.tvAppPackage.setText(appInfo.getAppPackage());                if (switchType.isChecked()) {                    //这里是直接展示appInfo提供的 应用名\包名\Icon                    holder.tvAppname.setText(appInfo.getAppname());                    holder.ivAppImage.setimageDrawable(appInfo.getAppIcon());                } else {                    //使用 指定资源数据                    holder.tvAppname.setText(appInfo.getValuesAppname());                    holder.ivAppImage.setimageDrawable(appInfo.getDrawablePreIEwimg());                }                return convertVIEw;            }        };        ListVIEw.setAdapter(applistadapter);    }    public static class VIEwHolder {        TextVIEw tvAppname;        TextVIEw tvAppPackage;        ImageVIEw ivAppImage;        public VIEwHolder(VIEw vIEw) {            tvAppname = vIEw.findVIEwByID(R.ID.tv_app_name);            tvAppPackage = vIEw.findVIEwByID(R.ID.tv_app_package);            ivAppImage = vIEw.findVIEwByID(R.ID.iv_app_icon);        }    }    public List<SimpleAppInfo> getAppsInfo() {        List<SimpleAppInfo> List = new ArrayList<>();        PackageManager pm = getPackageManager();        if (pm == null) return List;        List<PackageInfo> installedPackages = pm.getInstalledPackages(0);        for (PackageInfo pi : installedPackages) {            SimpleAppInfo ai = getBean(pm,pi);            if (ai == null) continue;            List.add(ai);        }        return List;    }    private SimpleAppInfo getBean(final PackageManager pm,final PackageInfo pi) {        if (pi == null) return null;        ApplicationInfo ai = pi.applicationInfo;        String packagename = pi.packagename;        String name = ai.loadLabel(pm).toString();        Drawable icon = ai.loadIcon(pm);        String packagePath = ai.sourceDir;        String versionname = pi.versionname;        int versionCode = pi.versionCode;        boolean isSystem = (ApplicationInfo.FLAG_SYstem & ai.flags) != 0;//        return new SimpleAppInfo(packagename,name,icon,packagePath,versionname,versionCode,isSystem);        SimpleAppInfo simpleAppInfo = new SimpleAppInfo(name,packagename,isSystem,versionCode);        try {            Resources resources = pm.getResourcesForApplication(packagename);            int appnameID = resources.getIDentifIEr("app_name","string",packagename);            if (appnameID > 0) {                String appname = resources.getString(appnameID);                simpleAppInfo.setValuesAppname(appname);            }            int prevIEwimgID = resources.getIDentifIEr("prevIEw_img","drawable",packagename);//                                    int prevIEwimgID = resources.getIDentifIEr("ic_launcher","drawable",packagename);            if (prevIEwimgID > 0) {                Drawable drawable = resources.getDrawable(prevIEwimgID);                simpleAppInfo.setDrawablePreIEwimg(drawable);            }        } catch (Exception e) {            e.printstacktrace();        }        return simpleAppInfo;    }}

所有代码均已贴出,并未使用任何第三方包,所以也就不上传demo了,

 

最后补充说明一下switch的作用

switch 开关 用于控制 显示  条目 内容 

switch  开 -> 显示默认的appname 和appIcon
switch  关 -> 显示指定的 app_name和prevIEw_img 资源 
  要是对你有帮助要关注我哦,我和其他人不一样,上代码就上全,绝不藏着掖着

@H_502_2@ 总结

以上是内存溢出为你收集整理的从手机中扫描以com keyboard themestudio为前缀的apk包,使用列表的形式展现全部内容,希望文章能够帮你解决从手机中扫描以com keyboard themestudio为前缀的apk包,使用列表的形式展现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1123377.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存