/**
* 判断服务是否正在运行
*
*
@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;
}

 


 

相关文章:

  • 2021-10-17
  • 2021-10-24
  • 2022-12-23
  • 2022-02-02
  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2021-07-08
猜你喜欢
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
  • 2022-12-23
相关资源
相似解决方案