当前位置:   article > 正文

getSystemService();的使用

getsystemservice

getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。下面介绍系统相应的服务:

传入的Name返回的对象说明
WINDOW_SERVICE WindowManager管理打开的窗口程序
LAYOUT_INFLATER_SERVICELayoutInflater 取得xml里定义的view
ACTIVITY_SERVICEActivityManager 管理应用程序的系统状态

POWER_SERVICE

PowerManger电源的服务
ALARM_SERVICEAlarmManager闹钟的服务

NOTIFICATION_SERVICE 

NotificationManager状态栏的服务
KEYGUARD_SERVICE KeyguardManager 键盘锁的服务
LOCATION_SERVICE LocationManager 位置的服务,如GPS
SEARCH_SERVICESearchManager 搜索的服务
VEBRATOR_SERVICE Vebrator手机震动的服务
CONNECTIVITY_SERVICEConnectivity网络连接的服务
WIFI_SERVICEWifiManagerWi-Fi服务
 TELEPHONY_SERVICETeleponyManager电话服务

 

代码示例,使用判断网络连接是否可用,并判断是否是wifi:

  1. protected int checkNetworkInfo() {
  2. ConnectivityManager con = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  3. NetworkInfo networkinfo = con.getActiveNetworkInfo();
  4. if (networkinfo == null || !networkinfo.isAvailable()) {
  5. // 当前网络不可用
  6. return 1;
  7. }
  8. boolean wifi = con.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
  9. .isConnectedOrConnecting();
  10. if (wifi) {
  11. // 使用wifi上网
  12. return 2;
  13. }
  14. return 3;
  15. }

转载:获取Android正在运行的任务和服务

检测某ActivityUpdate是否在当前Task的栈顶

  1. public static boolean isTopActivy(String cmdName, Context context)
  2. {
  3. ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  4. List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(Integer.MAX_VALUE);
  5. String cmpNameTemp = null;
  6. if (null != runningTaskInfos)
  7. {
  8. cmpNameTemp = (runningTaskInfos.get(0).topActivity).toString();
  9. }
  10. if (null == cmpNameTemp)
  11. {
  12. return false;
  13. }
  14. return cmpNameTemp.equals(cmdName);
  15. }

 

判断Android应用是否在前台

  1. public static boolean isAppOnForeground(Context context)
  2. {
  3. ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  4. String packageName = context.getPackageName();
  5. List<RecentTaskInfo> appTask = activityManager.getRecentTasks(Integer.MAX_VALUE, 1);
  6. if (appTask == null)
  7. {
  8. return false;
  9. }
  10. if (appTask.get(0).baseIntent.toString().contains(packageName))
  11. {
  12. return true;
  13. }
  14. return false;
  15. }
 

 

获取Android手机内安装的所有桌面

  1. private static List<String> getAllTheLauncher(Context context)
  2. {
  3. List<String> names = null;
  4. PackageManager pkgMgt = context.getPackageManager();
  5. Intent it = new Intent(Intent.ACTION_MAIN);
  6. it.addCategory(Intent.CATEGORY_HOME);
  7. List<ResolveInfo> ra = pkgMgt.queryIntentActivities(it, 0);
  8. if (ra.size() != 0)
  9. {
  10. names = new ArrayList<String>();
  11. }
  12. for (int i = 0; i < ra.size(); i++)
  13. {
  14. String packageName = ra.get(i).activityInfo.packageName;
  15. names.add(packageName);
  16. }
  17. return names;
  18. }

 

Android 判断程序前后台状态

  1. public static boolean isLauncherRunnig(Context context)
  2. {
  3. boolean result = false;
  4. List<String> names = getAllTheLauncher(context);
  5. ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  6. List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager.getRunningAppProcesses();
  7. for (RunningAppProcessInfo running : appList)
  8. {
  9. if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
  10. {
  11. for (int i = 0; i < names.size(); i++)
  12. {
  13. if (names.get(i).equals(running.processName))
  14. {
  15. result = true;
  16. break;
  17. }
  18. }
  19. }
  20. }
  21. return result;
  22. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/311055
推荐阅读
相关标签
  

闽ICP备14008679号