且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获得位置更新频繁的服务

更新时间:2023-12-04 15:20:58

我不知道什么是错的code。我看到它使用旧的位置提供。我首先建议使用新戏谷歌位置服务。更容易使用和更准确。

我有一个使用新的谷歌播放位置服务,并在后台工作的全面工作示例。它是MIT许可,并准备使用。

https://github.com/nickfox/GpsTracker/tree/master/ phoneClients / Android的

I am developing an android app, Getting location updates frequently by service.
My code is this

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

}

}  

I am using a switch to start a service. When i start the service by checking on the switch, the service gets the current location and toast the location frequently. I dont know what is wrong in my code and i dont know it gets the location or not.
somebody help me out.

My logcat doesnt show any Warnings or errors

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

I'm not sure what is wrong with your code. I see that it's using the old location provider. I would first suggest using the new google play location service. Much easier to use and more accurate.

I have a fully working sample that uses the new google play location services and works in the background. It's MIT licensed and ready to use.

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