Android里面常用的管理
2016-05-16 16:25
543 查看
Android常用Manager
1. ActivityManager:与系统中正在运行的所有活动进行交互。
获取ActivityManager对象的方法是在拥有context的环境下使用下面的方法。 ActivityManager activityManager= (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager常用的方法:
getRunningAppProcesses():获取系统中正在运行的所有的进程的信息。
getRunningServices:获取系统中正在运行的所有的服务的信息。
getMemoryInfo():获取系统内存信息。
getProcessMemoryInfo:获取某个或某几个进程占用的内存信息。
getDeviceConfigurationInfo():获取设备的配置属性。
2. FragmentManager:在Activity中与Fragment进行交互的接口
获取FragmentManager对象的方法是在Activity中使用下面的方法。 FragmentManager fragmentManager=getSupportFragmentManager();
AlarmManager常用方法:
getFragments():获取FragmentManager中所有的Fragment。
findFragmentById():通过id找到对应的Fragment。
beginTransaction():开启FragmentManager的事务。
FragmentManager只能直接对Fragment进行查询操作,不能直接进行增加,删除,更新操作,要进行这些操作必须在FragmentManager开启的事务中进行。开启的事务的任务都完成后要提交事务。
fragmentManager.beginTransaction().replace(R.id.fragment,fragment).commit();
3. PackageManager:检索当前安装在设备上的应用程序包相关的各种信息
获取PackageManager对象的方法是在在拥有context的环境下使用下面的方法。 PackageManager packageManager=getPackageManager();
PackageManager常用的方法:
getInstalledApplications():返回在设备上安装的所有应用程序包的列表。
getInstalledPackages():返回在设备上安装的所有包的列表。
getActivityInfo():获取对应组件名的Activity的信息。
注意其实PackageManager是一个抽象类。
4. DownloadManager:下载管理器是一个系统服务,处理长时间运行的HTTP下载
DownloadManager的基本使用方法:/** * DownloadManager的基本使用 */ public void downloadManager(){ //获取系统服务的DownloadManager downloadManager= (DownloadManager) getSystemService(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:关于网络连接状态的查询的类
ConnectivityManager的主要作用是:监视网络连接(Wi-Fi、GPRS、UMTS、等)
当网络连通性的变化发送广播意图
当连接的网络丢失,会自动连接别的网络
提供一个允许应用程序查询可用网络的粗粒度或细粒度的应用程序接口
提供一个允许应用程序请求和选择网络的应用程序的接口
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:应用程序使用的界面和窗口管理器
WindowManager是一个接口,基本使用方法如下:WindowManager windowManager=getWindowManager(); //向windowManager中添加视图 windowManager.addView(view); //删除windowManager中的视图 windowManager.removeView(view);
7. NotificationManager:通知用户发生的事件
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:提供访问设备上的电话服务的信息
获取TelephonyManager对象的方法是在拥有context的环境下使用下面的方法。 TelephonyManager telephonyManager= (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
TelephonyManager常用方法:
getCallState():返回电话状态。
TelephonyManager.CALL_STATE_IDLE无任何状态时
TelephonyManager.CALL_STATE_OFFHOOK接起电话时
TelephonyManager.CALL_STATE_RINGING电话进来时
getCellLocation():获取当前电话的位置
getDataActivity():获取数据活动状态
TelephonyManager.DATA_ACTIVITY_IN活动,正在接受数据
TelephonyManager.DATA_ACTIVITY_OUT活动,正在发送数据
TelephonyManager.DATA_ACTIVITY_INOUT活动,正在接受和发送数据
TelephonyManager.DATA_ACTIVITY_NONE活动,但无数据发送和接受
getDeviceId():返回设备id(当前移动终端的唯一标识)
getLine1Number():返回手机号码
9. LocationManager:提供了系统位置服务的访问
获取LocationManager对象的方法是在拥有context的环境下使用下面的方法。 LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationManager常用方法:
getAllProviders():获取所有能提供位置服务的Provider
getLastKnownLocation():获取上次开启位置服务记录的位置
requestLocationUpdates():注册位置更新的监听者
10. AlarmManager:提供系统报警服务的访问
获取AlarmManager对象的方法是在拥有context的环境下使用下面的方法。 AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager常用方法:
setTime(long millis):设置系统时钟时间。
setTimeZone(String timeZone):设置系统时钟时区。
setAlarmClock(AlarmClockInfo info, PendingIntent operation):设置一个警报来代表闹钟。
set(int type, long triggerAtMillis, PendingIntent operation):设置一次性闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟执行时间,第三个参数表示闹钟响应动作。
setRepeating(int type, long triggerAtMillis,long intervalMillis, PendingIntent operation):设置重复闹钟,第一个参数表示闹钟类型,第二个参数表示闹钟首次执行时间,第三个参数表示闹钟两次执行的间隔时间,第三个参数表示闹钟响应动作。
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个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间。
相关文章推荐
- android代码混淆
- Android 拍照或从相册取图片并裁剪
- Android之ListView
- Android SDK Manager 更新代理配置
- 在子线程中Toast
- Android Selector和Shape的组合
- Android实现推送方式解决方案
- Android Studio教程从入门到精通
- Android系统拨号盘按键发声方法ToneGenerator
- android查询几十万条数据的调研(一)
- Android ReallySmall MessageBroker(基于MQTT)
- 【ArcGIS for Android】经纬度坐标、地图投影坐标、屏幕坐标互相转换
- 查询地图软件,并选择导航
- Android学习笔记十三: layout布局属性、标签属性总结大全
- Android通用流行框架大全
- Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签
- android中Activity中的WindowManager与Window
- Android基于MQTT协议实现的推送功能
- Android Studio查看源代码报错
- Android Push Notification-androidpn(基于XMPP协议)