简体   繁体   English

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

[英]Getting Location Updates frequently in Service

I am developing an android app, Getting location updates frequently by service. 我正在开发一个Android应用程序,经常通过服务获取位置更新。
My code is this 我的代码是这样

MainActivity.java 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 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 我的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

To show your Toast, you must call show() after you make text. 要显示Toast,您必须在编写文本后调用show()。

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

Please also remove all these line: 请同时删除所有这些行:

@SuppressLint("ShowToast")

If you have any other errors , please post logcat. 如果您还有其他错误,请发布logcat。

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. 我首先建议使用新的Google Play定位服务。 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. 我有一个可以正常使用的示例,该示例使用了新的Google Play定位服务并在后台运行。 It's MIT licensed and ready to use. 它已获得MIT许可并可以使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM