一、结构图
二、管理器详解
(1)ActivityManager(活动管理器)
与系统中正在运行的【所有活动】进行交互
1、前提:获取ActivityManager对象的方法是在拥有context的环境下使用下面的方法
ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
2、ActivityManager常用的方法
- getRunningAppProcesses();获取系统中正在运行的所有的进程的信息
- getRunningServices();获取系统中正在运行的所有的服务的信息
- getMemoryInfo();获取系统内存信息
- getProcessMemoryInfo();获取某个或某几个进程占用的内存信息
- getDeviceConfigurationInfo();获取设备的配置属性
(2)FragmentManager(碎片管理器)
在【Activity中与Fragmen】t进行交互的接口
1、获取FragmentManager对象的方法是在Activity中使用下面的方法
FragmentManager fragmentManger = getSupportFragmentManager();
2、AlarmManager常用方法
- getFragment();获取FragmentManager中所有的Fragment
- findFragmentById();通过id找到对应的Fragment
- beginTransaction();开启FragmentManager的事务
FragmentManager只能直接对Fragment进行查询操作,不能直接进行增加,删除,更新操作,要进行这些操作必须在FragmentManager开启的事务中进行。开启的事务的任务都完成后要提交事务。
fragmentManager.beginTransaction().replace(R.id.fragment,fragment).commit();
(3)PackageManager(程序包管理器)
检索当前安装在设备上的应用程序包相关的各种信息
1、获取PackageManager对象的方法是在在拥有context的环境下使用下面的方法
PackageManager packageManager = getPackageManager();
2、PackageManager常用的方法
- getInstalledApplications(); 返回在设备上安装的所有应用程序包的列表
- getInstalledPackages(); 返回在设备上安装的所有包的列表
- getActivityInfo();获取对应组件名的Activity的信息
注意其实PackageManager是一个抽象类
(4)DownloadManager(下载管理器)
下载管理器是一个系统服务,处理【长时间运行】的HTTP下载
1、DownloadManager的基本使用方法
public void downloadManager(){
//获取系统服务的DownloadManager
downloadManager = (DownloadManager)getSystemvice(DOWNLOAD_SERVICE);
//创建一个DownloadManager的请求
DownloadManager.Request request=new DownloadManager.Request(Uri.parse("http://i.imgur.com/iXgyWbG.png"));
//设置请求允许的联网方式:移动网络与wifi都可以
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE| DownloadManager.Request.NETWORK_WIFI);
//禁止发出通知,既后台下载
//request.setShowRunningNotification(false);该方法被setNotificationVisibility取代了
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
//显示下载界面
request.setVisibleInDownloadsUi(true);
//设置下载后文件存放的位置,存放在/sdcard/Android/data/<包名>/files/Pictures目录下面
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_PICTURES, "iXgyWbG.png");
//将下载请求放入队列
downloadManager.enqueue(request);
}
(5)ConnectivityManager(连通性管理器)
关于网络连接状态的查询的类
1、ConnectivityManager的主要作用
- 监视网络连接(Wi-Fi、GPRS、UMTS、等)
- 当网络连通性的变化发送广播意图
- 当连接的网络丢失,会自动连接别的网络
- 提供一个允许应用程序查询可用网络的粗粒度或细粒度的应用程序接口
- 提供一个允许应用程序请求和选择网络的应用程序的接口
2、ConnectivityManager的基本使用方法
/**
* 检测当的网络(WLAN、3G/2G)状态
* @param context Context
* @return true 表示至少有一种网络处于连接状态
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
//网络信息
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected())
{
// 当前网络是连接的
if (info.getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
return false;
}
(6)WindowManager(视窗管理器)
应用程序使用的界面和窗口管理器
1、WindowManager是一个接口,基本使用方法如下
WindowManager windowManager = getWindowManager();
//向windowManager中添加视图
windowManager.addView(view);
//删除windowManager中的视图
windowManager.removeView(view);
(7)NotificationManager(通知管理器)
通知用户发生的事件
NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent intent=PendingIntent.getActivity(this,0,new Intent(MainActivity.this,MainActivity.class),0);
Notification notification=new NotificationCompat.Builder(this)
.setTicker("有新通知了")
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("通知标题")
.setContentText("通知内容")
.setWhen(0)
.setContentIntent(intent)
.build();
notificationManager.notify(0,notification);
(8)TelephonyManager(拨号服务管理器)
提供访问设备上的电话服务的信息
1、获取TelephonyManager对象的方法是在拥有context的环境下使用下面的方法
TelephonyManager telephonyManager= (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
2、TelephonyManager常用方法
1、getCallState():返回电话状态。
TelephonyManager.CALL_STATE_IDLE 无任何状态时
TelephonyManager.CALL_STATE_OFFHOOK 接起电话时
TelephonyManager.CALL_STATE_RINGING 电话进来时
2、getCellLocation():获取当前电话的位置
3、getDataActivity():获取数据活动状态
TelephonyManager.DATA_ACTIVITY_IN 活动,正在接受数据
TelephonyManager.DATA_ACTIVITY_OUT 活动,正在发送数据
TelephonyManager.DATA_ACTIVITY_INOUT 活动,正在接受和发送数据
TelephonyManager.DATA_ACTIVITY_NONE 活动,但无数据发送和接受
4、getDeviceId():返回设备id(当前移动终端的唯一标识)
5、getLine1Number():返回手机号码
(9)LocationManager(位置管理器)
提供了系统位置服务的访问
1、获取LocationManager对象的方法是在拥有context的环境下使用下面的方法
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
2、LocationManager常用方法
getAllProviders():获取所有能提供位置服务的Provider
getLastKnownLocation():获取上次开启位置服务记录的位置
requestLocationUpdates():注册位置更新的监听者
(10)AlarmManager(报警管理器)
提供系统报警服务的访问
1、获取AlarmManager对象的方法是在拥有context的环境下使用下面的方法
AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
2、AlarmManager常用方法
1、setTime(long millis):设置系统时钟时间。
2、setTimeZone(String timeZone):设置系统时钟时区。
3、setAlarmClock(AlarmClockInfo info, PendingIntent operation):设置一个警报来代表闹钟。
4、set(int type, long triggerAtMillis, PendingIntent operation):设置一次性闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟执行时间,第三个参数表示闹钟响应动作。
5、setRepeating(int type, long triggerAtMillis,long intervalMillis, PendingIntent operation):设置重复闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟首次执行时间,第三个参数表示闹钟两次执行的间隔时间,第三个参数表示闹钟响应动作。
6、setInexactRepeating(int type, long triggerAtMillis,long intervalMillis, PendingIntent operation):设置重复闹钟,与setRepeating方法类似,区别是setRepeating闹钟两次执行的间隔时间固定,而setInexactRepeating闹钟两次执行的间隔时间不固定。
上述方法中的type参数有五种值
AlarmManager.ELAPSED_REALTIME:表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始)。
AlarmManager.ELAPSED_REALTIME_WAKEUP:表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟也使用相对时间。
AlarmManager.RTC:表示闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间。
AlarmManager.RTC_WAKEUP:表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间。
AlarmManager.POWER_OFF_WAKEUP:表示闹钟在手机关机状态下也能正常进行提示功能,所以是5个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间。