Android判斷服務是否運行及定位問題實例分析
本文實例講述了Android判斷服務是否運行及定位問題。分享給大家供大家參考。具體如下:
/** * 判斷服務是否正在運行 * * @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開發(fā)中,經(jīng)常會使用locationManager.getLastKnownLocation()定時獲取經(jīng)緯度,在不同真機測試中有的可以獲取有的不可以獲取,為了解決不同手機的兼容下,請用如下代碼
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程序設計有所幫助。
相關(guān)文章
詳解Android中通過Intent類實現(xiàn)組件間調(diào)用的方法
Intent能夠?qū)崿F(xiàn)應用間的數(shù)據(jù)交互與通訊,將實現(xiàn)者和調(diào)用者解耦,接下來就來詳解Android中通過Intent類實現(xiàn)組件間調(diào)用的方法,需要的朋友可以參考下2016-05-05Android drawable微技巧,你不知道的drawable細節(jié)
今天小編就為大家分享一篇關(guān)于Android drawable微技巧,你不知道的drawable細節(jié),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10Android開發(fā)自學筆記(六):聲明權(quán)限和Activity
這篇文章主要介紹了Android開發(fā)自學筆記(六):聲明權(quán)限和Activity,本文是上一篇的補充,需要的朋友可以參考下2015-04-04避免 Android中Context引起的內(nèi)存泄露
本文主要介紹Android中Context引起的內(nèi)存泄露的問題,這里對Context的知識做了詳細講解,說明如何避免內(nèi)存泄漏的問題,有興趣的小伙伴可以參考下2016-08-08