使用服务在后台进行 GPS 跟踪

发布于 2024-12-19 03:24:31 字数 2497 浏览 1 评论 0原文

我有一个用于监听 GPS 位置的服务,我想在 Toast 中显示更新的位置,我通过单击按钮在其他活动中运行该服务。该服务已成功创建,但未显示更新位置的 toast。这是代码:

   public class locationlistening_service extends Service implements LocationListener  {

    static LocationManager locationManager;
    LocationListener locationListener;

    private static final String TAG = "Calculations";
    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");
        run();
    }

    private void run(){

        final Criteria criteria = new Criteria();

        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setSpeedRequired(true);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        //Acquire a reference to the system Location Manager
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        // Define a listener that responds to location updates
        locationListener = new LocationListener() {
              public void onLocationChanged(Location location) 
              {
                 recordLocation(location);
              }

              public void onStatusChanged(String provider, int status, Bundle extras) {}

              public void onProviderEnabled(String provider) {}

              public void onProviderDisabled(String provider) {}

        };
    }
public void recordLocation(Location loc) 
{
Toast.makeText(locationlistening_service.this,"Lat: " + String.valueOf(loc.getLatitude()) + " Long: " + String.valueOf(loc.getLongitude()),Toast.LENGTH_SHORT).show();

}



    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }



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

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

        }

其他活动中有服务调用。

public void startButton(View view) {

    startService(new Intent(this, locationlistening_service.class));
}

I have a service for listening the GPS locations and I want to display an updated location in Toast, I run the service in other activity by clicking a Button. The service is created successfully but not showing the toast of updated location. Here is code:

   public class locationlistening_service extends Service implements LocationListener  {

    static LocationManager locationManager;
    LocationListener locationListener;

    private static final String TAG = "Calculations";
    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");
        run();
    }

    private void run(){

        final Criteria criteria = new Criteria();

        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setSpeedRequired(true);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        //Acquire a reference to the system Location Manager
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        // Define a listener that responds to location updates
        locationListener = new LocationListener() {
              public void onLocationChanged(Location location) 
              {
                 recordLocation(location);
              }

              public void onStatusChanged(String provider, int status, Bundle extras) {}

              public void onProviderEnabled(String provider) {}

              public void onProviderDisabled(String provider) {}

        };
    }
public void recordLocation(Location loc) 
{
Toast.makeText(locationlistening_service.this,"Lat: " + String.valueOf(loc.getLatitude()) + " Long: " + String.valueOf(loc.getLongitude()),Toast.LENGTH_SHORT).show();

}



    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }



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

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

        }

there is call of service in other activity.

public void startButton(View view) {

    startService(new Intent(this, locationlistening_service.class));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2024-12-26 03:24:31

您永远不会调用 requestLocationUpdates()。因此,没有任何内容使用您的 LocationManagerLocationListenerCriteria

当不再需要该服务时,请务必关闭该服务。

You are never calling requestLocationUpdates(). Hence, nothing is using your LocationManager, LocationListener, or Criteria.

Please be sure to shut down this service when it is no longer needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文