三十八、判断服务是否运行及定位问题

    /**
* 判断服务是否正在运行
*
*
@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()定时获取经纬度,在不同真机测试中有的可以获取有的不可以获取,为了解决不同手机的兼容下,请用如下代码

    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;
}

 


 

posted on 2011-12-01 17:22  Ruthless  阅读(1219)  评论(0编辑  收藏  举报