本文实例讲述了Android判断服务是否运行及定位问题。分享给大家供大家参考。具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/**
* 判断服务是否正在运行
*
* @param context
* @param className 判断的服务名字:包名+类名
* @return true在运行 false 不在运行
*/
public static boolean isServiceRunning(Context context, String className) {
boolean isRunning = false ;
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
//获取所有的服务
List<ActivityManager.RunningServiceInfo> services= activityManager.getRunningServices(Integer.MAX_VALUE);
if (services!= null &&services.size()> 0 ){
for (ActivityManager.RunningServiceInfo service : services){
if (className.equals(service.service.getClassName())){
isRunning= true ;
break ;
}
}
}
return isRunning;
}
|
在android开发中,经常会使用locationManager.getLastKnownLocation()定时获取经纬度,在不同真机测试中有的可以获取有的不可以获取,为了解决不同手机的兼容下,请用如下代码
1
2
3
4
5
6
7
8
9
10
|
public static Location getLocation(LocationManager locationManager, LocationListener locationListener) {
Location location= null ;
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0 , locationListener);
if (location== null ){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0 , 0 , locationListener);
}
return location;
}
|
希望本文所述对大家的Android程序设计有所帮助。