Java 在服务中频繁获取位置更新

Java 在服务中频繁获取位置更新,java,android,gps,location,Java,Android,Gps,Location,我正在开发一个android应用程序,通过服务频繁地获取位置更新。 我的密码是 MainActivity.java import android.app.Activity; import android.content.Intent; import android.location.LocationManager; import android.os.Bundle; import android.provider.Settings; import android.view.Menu; impor

我正在开发一个android应用程序,通过服务频繁地获取位置更新。
我的密码是

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View view = this.getWindow().getDecorView();
    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabled = service
      .isProviderEnabled(LocationManager.GPS_PROVIDER);

    // check if enabled and if not send user to the GSP settings
    // Better solution would be to display a dialog and suggesting to 
    // go to the settings
    if (!enabled) {
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(intent);
    } 
    Switch s = (Switch) findViewById(R.id.switch1);
    if(s != null) {
        s.setOnCheckedChangeListener(this);
    }


}

public void onResume(View v) {

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public void onCheckedChanged(CompoundButton buttonview, boolean isChecked) {
    // TODO Auto-generated method stub
    if(isChecked) {
        Intent intent = new Intent(this, GetGeoLocation.class);
        startService(intent);
    }

    if(!isChecked) {
        stopService(new Intent(MainActivity.this,GetGeoLocation.class));
    }
}

}
GetGeoLocation.java

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class GetGeoLocation extends Service implements LocationListener{


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

public void onCreate() {
    Toast.makeText(this, "The new Service was Created", Toast.LENGTH_SHORT).show();
}
public int onStart() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    Criteria cri = new Criteria();
    String bestProvider = locationManager.getBestProvider(cri, true);

    Location loc = locationManager.getLastKnownLocation(bestProvider);
    double latMy = loc.getLatitude();
    double lngMy = loc.getLongitude();
    Toast.makeText(this, " " +latMy+"  "+lngMy, Toast.LENGTH_SHORT).show();
    return START_STICKY;
}

public void onDestroy() {
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();

}

@SuppressLint("ShowToast")
@Override
public void onLocationChanged(Location loc) {
    // TODO Auto-generated method stub
    Toast.makeText(this,"Location Changed", Toast.LENGTH_SHORT);
    Toast.makeText(this, " " +loc.getLatitude()+"  "+loc.getLongitude(), Toast.LENGTH_SHORT).show();
    System.out.println("Location"+loc.getLatitude()+"  " +loc.getLongitude());
}

@SuppressLint("ShowToast")
@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this,"ProviderDisabled", Toast.LENGTH_SHORT);
}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

}  
我正在使用交换机启动服务。当我通过检查交换机来启动服务时,该服务会获取当前位置并频繁地吐司该位置。我不知道我的代码出了什么问题,也不知道它是否得到了位置。
谁来帮帮我

我的logcat没有显示任何警告或错误

01-29 20:29:34.996: I/SurfaceTextureClient(5512): [STC::queueBuffer] (this:0x5b79b008) fps:0.14, dur:7126.33, max:7126.33, min:7126.33
01-29 20:29:34.997: I/SurfaceTextureClient(5512): [STC::queueBuffer] this:0x5b79b008, api:1, last queue time elapsed:7126.33
01-29 20:29:35.268: D/skia(5512): Flag is not 10

若要显示祝酒词,必须在生成文本后调用show()

Toast.makeText(...).show();
请同时删除所有这些行:

@SuppressLint("ShowToast")

如果您有任何其他错误,请发布logcat。

我不确定您的代码出了什么问题。我看到它正在使用旧的位置提供程序。我首先建议使用新的google play定位服务。更容易使用和更准确

我有一个完全可以工作的示例,它使用新的google play定位服务,并在后台工作。它是麻省理工学院授权的,可以随时使用


如果答案解决了您的问题,请接受。如果没有,请提供更多信息或您的解决方案。或者,如果不再需要这个问题,就删除它。