赞
踩
getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。下面介绍系统相应的服务:
传入的Name | 返回的对象 | 说明 |
WINDOW_SERVICE | WindowManager | 管理打开的窗口程序 |
LAYOUT_INFLATER_SERVICE | LayoutInflater | 取得xml里定义的view |
ACTIVITY_SERVICE | ActivityManager | 管理应用程序的系统状态 |
POWER_SERVICE | PowerManger | 电源的服务 |
ALARM_SERVICE | AlarmManager | 闹钟的服务 |
NOTIFICATION_SERVICE | NotificationManager | 状态栏的服务 |
KEYGUARD_SERVICE | KeyguardManager | 键盘锁的服务 |
LOCATION_SERVICE | LocationManager | 位置的服务,如GPS |
SEARCH_SERVICE | SearchManager | 搜索的服务 |
VEBRATOR_SERVICE | Vebrator | 手机震动的服务 |
CONNECTIVITY_SERVICE | Connectivity | 网络连接的服务 |
WIFI_SERVICE | WifiManager | Wi-Fi服务 |
TELEPHONY_SERVICE | TeleponyManager | 电话服务 |
代码示例,使用判断网络连接是否可用,并判断是否是wifi:
- protected int checkNetworkInfo() {
- ConnectivityManager con = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkinfo = con.getActiveNetworkInfo();
- if (networkinfo == null || !networkinfo.isAvailable()) {
- // 当前网络不可用
- return 1;
- }
- boolean wifi = con.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
- .isConnectedOrConnecting();
- if (wifi) {
- // 使用wifi上网
- return 2;
- }
- return 3;
- }
检测某ActivityUpdate是否在当前Task的栈顶
- public static boolean isTopActivy(String cmdName, Context context)
- {
- ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
- List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(Integer.MAX_VALUE);
- String cmpNameTemp = null;
- if (null != runningTaskInfos)
- {
- cmpNameTemp = (runningTaskInfos.get(0).topActivity).toString();
- }
-
- if (null == cmpNameTemp)
- {
- return false;
- }
-
- return cmpNameTemp.equals(cmdName);
-
- }
判断Android应用是否在前台
- public static boolean isAppOnForeground(Context context)
- {
- ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
- String packageName = context.getPackageName();
- List<RecentTaskInfo> appTask = activityManager.getRecentTasks(Integer.MAX_VALUE, 1);
-
- if (appTask == null)
- {
- return false;
- }
-
- if (appTask.get(0).baseIntent.toString().contains(packageName))
- {
- return true;
- }
- return false;
- }
获取Android手机内安装的所有桌面
- private static List<String> getAllTheLauncher(Context context)
- {
- List<String> names = null;
- PackageManager pkgMgt = context.getPackageManager();
- Intent it = new Intent(Intent.ACTION_MAIN);
- it.addCategory(Intent.CATEGORY_HOME);
- List<ResolveInfo> ra = pkgMgt.queryIntentActivities(it, 0);
- if (ra.size() != 0)
- {
- names = new ArrayList<String>();
- }
- for (int i = 0; i < ra.size(); i++)
- {
- String packageName = ra.get(i).activityInfo.packageName;
- names.add(packageName);
- }
- return names;
- }
Android 判断程序前后台状态
- public static boolean isLauncherRunnig(Context context)
- {
- boolean result = false;
- List<String> names = getAllTheLauncher(context);
- ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
- List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager.getRunningAppProcesses();
- for (RunningAppProcessInfo running : appList)
- {
- if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
- {
- for (int i = 0; i < names.size(); i++)
- {
- if (names.get(i).equals(running.processName))
- {
- result = true;
- break;
- }
- }
- }
- }
- return result;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。