2013-08-05 146 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