Android进阶-各种Manager(一)

本文介绍了 Android 开发中几个关键的 Manager 类,包括 PackageManager、ActivityManager 和 LocationManager 的使用方法和示例。这些 Manager 类提供了从获取应用信息到管理运行中的应用及获取设备地理位置等强大的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android进阶-各种Manager(一)

PackageManager

这个类可以用于获取各种已经安装了的应用包的信息,
简单使用:
/这个类方法,将得到系统所有应用的信息/

    public static Map<String, List<AppInfo>> getSystemAppInfo(Context context){

        Map<String, List<AppInfo>>  appInfos = new HashMap<>();
        List<AppInfo> userApp = new ArrayList<>();
        List<AppInfo> sysApp = new ArrayList<>();

        PackageManager pm = context.getPackageManager();
        List<PackageInfo> packInfos = pm.getInstalledPackages(0);
        AppInfo appinfo = null;

        for(PackageInfo packInfo : packInfos){
            appinfo = new AppInfo();
            String packname = packInfo.packageName;  //得到包名

            Drawable icon = packInfo.applicationInfo.loadIcon(pm);  //得到应用图标

            String appname = packInfo.applicationInfo.loadLabel(pm).toString(); //得到应用名

            //应用程序apk包的路径
            String apkpath = packInfo.applicationInfo.sourceDir;  //得到应用路径
            appinfo.setApkpath(apkpath);
            long appSize = new File(apkpath).length();    //进而得到应用大小


            //应用程序安装的位置。
            int flags = packInfo.applicationInfo.flags; //二进制映射  大bit-map
            if((ApplicationInfo.FLAG_EXTERNAL_STORAGE&flags)!=0){   //判断应用的安装位置
                //外部存储
                appinfo.setInRom(false);
            }else{
                //手机内存
                appinfo.setInRom(true);
            }
            /*封装得到的数据*/
        }

        appInfos.put("userApp", userApp);
        appInfos.put("sysApp", sysApp);
        return appInfos;
    }

ActivityManager

用来管理,系统中所有正在运行的应用。还可以用它来获得手机的基本信息
使用范例:

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    am.getRunningServices(200);     //获取手机中正在运行的服务, 200代表返回的最大数量
    am.getRunningAppProcesses();    //获取手机上正在运行的进程
    am.getMemoryInfo(new ActivityManager.MemoryInfo()); //获取手机内存的基本信息  ,但是这个API只在高版本可用
    am.getDeviceConfigurationInfo(); //获取手机的配置信息
    am.killBackgroundProcess();  //消灭进程
    .....

上面说使用ActivityManager 获取手机内存的信息只能在高版本使用,那么怎么在低版本中获得这个信息呢。
大家都知道android的底层是linux, 在linux中的proc文件夹存放着当前系统运行进程的信息, 因此我们可以通过
/proc文件夹下的内容,来获得手机运行的一些信息,例如下面这段代码,就能获得手机的内存大小

    public static long getTotalMem(Context context) {
            //这里我们要获得的总内存,位于/proc/meminfo文件中的第一行, 因此只要读出第一行,再做处理就可以了
            // 第一行内容范例: MemTotal: 344740 kB "
            try {
                // /proc/meminfo 配置文件的路径
                FileInputStream fis = new FileInputStream(new File("/proc/meminfo"));
                BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
                String readLine = reader.readLine();
                StringBuffer sb = new StringBuffer();
                for (char c : readLine.toCharArray()) {
                    if (c >= '0' && c <= '9') {
                        sb.append(c);
                    }
                }
                return Long.parseLong(sb.toString()) * 1024;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return 0;
        }

LocationManeger

This class provides access to the system location services. These services allow applications to obtain periodic updates of the
device’s geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.
即,这个类可以帮助使用系统的Location服务,通过这个服务我们可以实时更新我们设备的物理位置,并且还可以当我们的设备到达一个特定的物理位置是,做我们要做的事
注意,手机定位的方式有多种:GPS、AGPS、网络定位

使用范例:

    @Override
    public void onCreate() {
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);  //获取LocationManager
        listener = new MyListener();
        Criteria criteria = new Criteria(); //criteria 查询条件
        criteria.setAccuracy(Criteria.ACCURACY_FINE);//获取准确的位置。
        criteria.setCostAllowed(true);//允许产生开销
        String  name = lm.getBestProvider(criteria, true);      //true只返回可用的位置提供者 
        System.out.println("最好的位置提供者:"+name);
        lm.requestLocationUpdates(name, 0, 0, listener); //设置定位监听
        super.onCreate();
    }

    private class MyListener implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {
            StringBuilder sb = new StringBuilder();
            System.out.println("精确度:"+location.getAccuracy());
            System.out.println("移动的速度:"+location.getSpeed());
            System.out.println("纬度:"+location.getLatitude());
            System.out.println("经度:"+location.getLongitude());
            System.out.println("海拔:"+location.getAltitude());
            /*.....*/
        }
        //当位置提供者 状态发生变化的时候调用的方法。
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }
        //当某个位置提供者 可用的时候调用的方法。
        @Override
        public void onProviderEnabled(String provider) {

        }
        //当某个位置提供者 不可用的时候调用的方法。
        @Override
        public void onProviderDisabled(String provider) {

        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值