Android 获取应用包名

Android 获取应用包名

得知华为把荣耀手机卖了, 看着我手中的荣耀30,突然想到会不会给我的手机推送一些奇奇怪怪的东西, 为了保险起见,决定把系统更新禁用了

然鹅,我竟然在华为的系统里看不到应用包名, 苦寻无果后,决定写一个查看包名的工具

经历了两个小时的面向搜索引擎编程 。。。。。。

最终效果图

下面贴上相关代码

AppManageList
/**
 * 应用名称,包名图标展示
 */
public class AppManageList extends BaseActivity {

    private ListView listView;
    private List<AppManageListBean> list;
    private AppManageListAdapter adapter;
    private ClipboardManager cm;
    private ClipData mClipData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_app_manage_list);

    }

    @Override
    public String getTitleText() {
        return "应用信息列表";
    }

    @Override
    public void initView() {
        listView = findViewById(R.id.act_app_manage_list_lv);

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        adapter = new AppManageListAdapter(this, list);
        listView.setAdapter(adapter);
        //长按条目复制包名
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取剪贴板管理器:
                cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                mClipData = ClipData.newPlainText("packageName", list.get(position).getPackageName());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                showToast("包名复制成功");
            }
        });


        //测试
//        for (int i = 0; i < 5; i++) {
//            list.add(new AppManageListBean("app的mignzi", "akdsfjlaklsdjflkasdf", "110MB", "11.10"));
//        }
//        adapter.notifyDataSetChanged();

    }


    @Override
    protected void onResume() {
        showProgress("正在加载应用信息");
        new Thread(new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void run() {
                getItems(AppManageList.this, new CallBackListener<List<AppManageListBean>>() {
                    @Override
                    public void onSuccess(List<AppManageListBean> itemList) {

                        list.clear();
                        list.addAll(itemList);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                hideProgress();
                                adapter.notifyDataSetChanged();
                            }
                        });

                    }

                    @Override
                    public void onError(String error) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                hideProgress();
                            }
                        });

                    }
                });
            }
        }).start();
        super.onResume();

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void getItems(Context context, CallBackListener<List<AppManageListBean>> listener) {

        PackageManager pckMan = context.getPackageManager();
        ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
        List<AppManageListBean> itemAppManage = new ArrayList<>();
        List<PackageInfo> packageInfo = pckMan.getInstalledPackages(PackageManager.GET_ACTIVITIES);

        for (PackageInfo appInfo : packageInfo) {

            StorageStats packageStats = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                packageStats = AppSizeUtil.getPackageStats(this, appInfo.packageName);
            }
            itemAppManage.add(new AppManageListBean(appInfo.applicationInfo.loadIcon(pckMan), appInfo.applicationInfo.loadLabel(pckMan).toString()
                    , appInfo.packageName, packageStats == null ? "" : String.valueOf(packageStats.getAppBytes()), appInfo.versionName));
        }
        listener.onSuccess(itemAppManage);
//        return items;
    }

}
act_app_manage_list.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=".view.work.AppManageList">


    <include layout="@layout/include_common_title" />

    <ListView
        android:id="@+id/act_app_manage_list_lv"
        android:layout_width="match_parent"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        android:layout_height="wrap_content" />





</LinearLayout>
AppManageListAdapter
/**
 * 应用管理列表适配器
 * @author  zhou
 */
public class AppManageListAdapter extends BaseAdapter {

    private Context context;
    private List<AppManageListBean> list;


    public AppManageListAdapter(Context context, List<AppManageListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (list != null) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        if (list != null) {
            return position;
        }
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.list_item_app_manage, null);
            viewHolder.list_item_app_manage_app_name_tv = convertView.findViewById(R.id.list_item_app_manage_app_name_tv);
            viewHolder.list_item_app_manage_app_package_name_tv = convertView.findViewById(R.id.list_item_app_manage_app_package_name_tv);
            viewHolder.list_item_app_manage_app_version_code_tv = convertView.findViewById(R.id.list_item_app_manage_app_version_code_tv);
            viewHolder.list_item_app_manage_app_size_tv = convertView.findViewById(R.id.list_item_app_manage_app_size_tv);
            viewHolder.list_item_app_manage_icon_iv = convertView.findViewById(R.id.list_item_app_manage_icon_iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.list_item_app_manage_app_name_tv.setText(list.get(position).getAppName());
        viewHolder.list_item_app_manage_app_version_code_tv.setText("版本号:"+list.get(position).getVersionCode());
//        viewHolder.list_item_app_manage_app_size_tv.setText("大小:"+list.get(position).getPackageSize());
        viewHolder.list_item_app_manage_app_size_tv.setVisibility(View.GONE);
        viewHolder.list_item_app_manage_app_package_name_tv.setText("包名:"+list.get(position).getPackageName());
        viewHolder.list_item_app_manage_icon_iv.setImageDrawable(list.get(position).getIconIv());
        return convertView;
    }


    public class ViewHolder{
        TextView list_item_app_manage_app_version_code_tv;
        TextView list_item_app_manage_app_size_tv;
        TextView list_item_app_manage_app_package_name_tv;
        TextView list_item_app_manage_app_name_tv;
        ImageView list_item_app_manage_icon_iv;
    }

}
list_item_app_manage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!--    根目录容器-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dp"
        android:orientation="horizontal">

        <!--        左侧应用图标-->
        <ImageView
            android:id="@+id/list_item_app_manage_icon_iv"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"

            />

        <!--        右侧应用信息容器-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_marginTop="3dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="3dp"
            android:orientation="vertical">
            <!--            第一行应用名称-->
            <TextView
                android:id="@+id/list_item_app_manage_app_name_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dp"
                android:singleLine="true"
                android:textSize="18sp" />
            <!--            第二行应用包名-->
            <TextView
                android:id="@+id/list_item_app_manage_app_package_name_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dp"
                android:singleLine="true"
                android:textSize="14sp" />
            <!--            第三行应用大小和应用版本号-->
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
<!--                应用大小-->
                <TextView
                    android:id="@+id/list_item_app_manage_app_size_tv"
                    android:layout_width="wrap_content"
                    android:layout_marginRight="10dp"
                    android:textSize="14sp"
                    android:layout_height="wrap_content">

                </TextView>
<!--                应用版本号-->
                <TextView
                    android:id="@+id/list_item_app_manage_app_version_code_tv"
                    android:layout_width="wrap_content"
                    android:textSize="14sp"
                    android:layout_height="wrap_content">

                </TextView>
            </LinearLayout>



        </LinearLayout>


    </LinearLayout>


</LinearLayout>

CallBackListener

public interface CallBackListener<T> {
    void onSuccess(T t);

    void onError(String error);
}

 

评论 1 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术工厂 设计师:CSDN官方博客 返回首页

打赏作者

lanyu96

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值