android - 使用服务获取 GPS 位置,android?

标签 android service gps location

这是我第一次使用服务,从 Activity 来看它确实看起来很复杂。

因此,我试图在用户关闭我的服务应用程序后获取用户的位置。

这是我的服务类别。

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();

     }

}
}

请告诉我我做错了什么。 如果我在 Activity 类中使用代码,那么我可以在 logcat 中获取地址,但在使用服务时却无法获取地址。

这就是我从 Activity 调用服务的方式

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

最佳答案

最好的方法是使用 CWAC 的定位服务 Location Poller 服务已经为我们提供了使用它,只需给出唤醒它的时间间隔

像这样操作,您将需要 jar 文件,您可以从 https://www.dropbox.com/sh/pgxk2v9l5vl0h2j/3svyZnuwOK/CWAC-LocationPoller.jar 获取它。

从您的 Activity 中启动 LocationPoller 并将闹钟设置为您想要的时间重复

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

创建一个接收器类位置接收器,从中获取经纬度

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

并将其添加到您的 list 中

   <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" />

关于android - 使用服务获取 GPS 位置,android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18051958/

相关文章:

android - 在触摸自动完成编辑文本建议时隐藏软键盘

android - 以编程方式更改蓝牙音量?

android - 读取服务中 Activity 存储的 SharedPreferences?

android - 如何解决服务实例问题?

Java Jersey RESTful 服务

android - 如何实现GPS状态变化监听器

c++ - 用于将 GPS 坐标转换为 NMEA 格式的 AVR 代码

android - 动态位置布局

c# - 车辆 GPS 跟踪设备

android - ViewPager 花时间加载包含 ListViews 的 fragment