您的位置:首页 > 移动开发 > Android开发

Android进阶-各种Manager(一)

2015-11-19 20:32 513 查看

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) {

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android