在服务中经常获取位置更新

时间:2014-01-29 14:41:26

标签: 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;
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

2 个答案:

答案 0 :(得分:0)

要显示Toast,您必须在创建文本后调用show()。

Toast.makeText(...).show();

请同时删除所有这些内容:

@SuppressLint("ShowToast")

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

答案 1 :(得分:0)

我不确定您的代码有什么问题。我看到它正在使用旧的位置提供商。我首先建议使用新的Google Play定位服务。更容易使用,更准确。

我有一个完整的工作样本,它使用新的Google Play定位服务并在后台运行。这是麻省理工学院的许可并且可以随时使用。

https://github.com/nickfox/GpsTracker/tree/master/phoneClients/android

相关问题