2013-08-05 145 views
1

這是我第一次使用服務,它肯定看起來很複雜。使用服務獲取GPS位置,android?

所以我想在用服務關閉我的應用程序之後獲取用戶的位置。

這是我的服務課。

public class LocTrack extends Service { 


GPSTracker gp; 
@Override 
public void onCreate() 
{ gp = new GPSTracker(getApplicationContext()); 
    onLocationChanged(gp); 
    super.onCreate();   
} 
@Override 
public void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // TODO Auto-generated method stub 

    return Service.START_STICKY; 
} 


@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

public void onLocationChanged(GPSTracker track) { 


    // Getting latitude 
    double latitude = track.getLatitude(); 

    // Getting longitude 
    double longitude = track.getLongitude(); 

    Geocoder geocoder = new Geocoder(LocTrack.this, Locale.getDefault()); 

    try 
    { 
     List<Address> addresses = geocoder.getFromLocation(latitude, longitude,1); 
     Log.e("Addresses","-->"+addresses); 

    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 

    } 

} 
} 

請告訴我我做錯了什麼。 如果我在活動類中使用代碼,那麼我可以在logcat中獲取地址,但不能在使用服務時獲取地址。

這就是我如何調用從活動服務

Intent intent = new Intent(MainActivity.this, LocTrack.class); 
      startService(intent); 
+0

你有沒有實施清單 –

+0

的權限是.....LocTrack是我的服務級別的名稱 – WISHY

+0

否M互聯網,GPS,位置,模擬 –

回答

2

,最好的辦法是通過CWAC使用位置服務 位置輪詢服務已經取得了我們使用它,你必須給時間間隔來喚醒它

做到像DIS方式n您需要的jar文件,你可以從https://www.dropbox.com/sh/pgxk2v9l5vl0h2j/3svyZnuwOK/CWAC-LocationPoller.jar

把它從你的活動開始LocationPoller並設置報警REPE阿婷您要

AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE); 
Intent i = new Intent(this, LocationPoller.class); 

Bundle bundle = new Bundle(); 
LocationPollerParameter parameter = new LocationPollerParameter(bundle); 
parameter.setIntentToBroadcastOnCompletion(new Intent(this, 
     LocationReceiver.class)); 
// try GPS and fall back to NETWORK_PROVIDER 
parameter.setProviders(new String[] { LocationManager.GPS_PROVIDER, 
     LocationManager.NETWORK_PROVIDER }); 
parameter.setTimeout(120000); 
i.putExtras(bundle); 

PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0); 
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
     SystemClock.elapsedRealtime(), 300000, pi); 

請從這裏您可以獲取LATñLON

public class LocationReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 


    try {  
     Bundle b=intent.getExtras(); 

     LocationPollerResult locationResult = new LocationPollerResult(b); 

     Location loc=locationResult.getLocation(); 
     String msg; 

     if (loc==null) { 
     loc=locationResult.getLastKnownLocation(); 

     if (loc==null) { 
      msg=locationResult.getError(); 
     } 
     else { 
      msg="TIMEOUT, lastKnown="+loc.toString(); 
     } 
     } 
     else { 
     msg=loc.toString(); 



     Log.i("Location Latitude", String.valueOf(loc.getLatitude())); 
     Log.i("Location Longitude", String.valueOf(loc.getLongitude())); 
     Log.i("Location Accuracy", String.valueOf(loc.getAccuracy())); 




     } 

     Log.i(getClass().getSimpleName(), "received location: " + msg); 



    } 
    catch (Exception e) { 
     Log.e(getClass().getName(), e.getMessage()); 
    } 
    } 

,這增加了接收機類定位接收機的時間您的清單

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <receiver android:name="com.commonsware.cwac.locpoll.LocationPoller" /> 

    <service android:name="com.commonsware.cwac.locpoll.LocationPollerService" /> 

和接收者聲明,您將獲取所有內容

<receiver android:name="com.RareMediaCompany.Helios.LocationReceiver" /> 
+0

它無法找到LocationPollerResult和LocationPollerParameter ....這些是什麼? – WISHY

+0

Srry那是一個過時的庫...用這個n代替以前的它會工作... [link] https://www.dropbox.com/sh/pgxk2v9l5vl0h2j/3svyZnuwOK/CWAC-LocationPoller.jar –

+0

I認爲它不是工作或我做錯了什麼.....我複製粘貼您的代碼,但我無法獲得任何位置在logcat ... – WISHY