热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java更新位置_java使用翻新库更新GPS位置并将坐标发送到服务器(在后台服务中)堆栈内存溢出...

我是使用后台服务和改造库的新手,通过调试我的应用程序,我没有收到任何错误,我知道它的获取坐标但未发送到服务器(在后台服务中)任何帮助将不胜

我是使用后台服务和改造库的新手,通过调试我的应用程序,我没有收到任何错误,我知道它的获取坐标但未发送到服务器(在后台服务中)任何帮助将不胜感激,在此先感谢,祝您编程愉快!

GPS服务

public class LocationUpdaterService extends Service

{

public static final int TWO_MINUTES = 120000; // 120 seconds

public static Boolean isRunning = false;

public LocationManager mLocationManager;

public LocationUpdaterListener mLocationListener;

public Location previousBestLocation = null;

@Nullable

@Override

public IBinder onBind(Intent intent) {

return null;

}

@Override

public void onCreate() {

mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

mLocationListener = new LocationUpdaterListener();

super.onCreate();

}

Handler mHandler = new Handler();

Runnable mHandlerTask = new Runnable(){

@Override

public void run() {

if (!isRunning) {

startListening();

}

mHandler.postDelayed(mHandlerTask, TWO_MINUTES);

}

};

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

mHandlerTask.run();

return START_STICKY;

}

@Override

public void onDestroy() {

stopListening();

mHandler.removeCallbacks(mHandlerTask);

super.onDestroy();

}

private void startListening() {

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED

|| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

if (mLocationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))

mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);

if (mLocationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);

}

isRunning = true;

}

private void stopListening() {

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED

|| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

mLocationManager.removeUpdates(mLocationListener);

}

isRunning = false;

}

public class LocationUpdaterListener implements LocationListener

{

@Override

public void onLocationChanged(Location location) {

if (isBetterLocation(location, previousBestLocation)) {

previousBestLocation = location;

try {

// Script to post location data to server..

Call loginCall;

String deviceKey;

deviceKey = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

loginCall = MyApplication.getInstance().getAPI().update(deviceKey,String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude()));

loginCall.enqueue(new Callback() {

@Override

public void onResponse(Call call, Response response) {

if(response.getClass() != null)

{

}

}

@Override

public void onFailure(Call call, Throwable t) {

}

});

}

catch (Exception e) {

e.printStackTrace();

}

finally {

stopListening();

}

}

}

@Override

public void onProviderDisabled(String provider) {

stopListening();

}

@Override

public void onProviderEnabled(String provider) { }

@Override

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

}

protected boolean isBetterLocation(Location location, Location currentBestLocation) {

if (currentBestLocation == null) {

// A new location is always better than no location

return true;

}

// Check whether the new location fix is newer or older

long timeDelta = location.getTime() - currentBestLocation.getTime();

boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;

boolean isSignificantlyOlder &#61; timeDelta <-TWO_MINUTES;

boolean isNewer &#61; timeDelta > 0;

// If it&#39;s been more than two minutes since the current location, use the new location

// because the user has likely moved

if (isSignificantlyNewer) {

return true;

// If the new location is more than two minutes older, it must be worse

} else if (isSignificantlyOlder) {

return false;

}

// Check whether the new location fix is more or less accurate

int accuracyDelta &#61; (int) (location.getAccuracy() - currentBestLocation.getAccuracy());

boolean isLessAccurate &#61; accuracyDelta > 0;

boolean isMoreAccurate &#61; accuracyDelta <0;

boolean isSignificantlyLessAccurate &#61; accuracyDelta > 200;

// Check if the old and new location are from the same provider

boolean isFromSameProvider &#61; isSameProvider(location.getProvider(), currentBestLocation.getProvider());

// Determine location quality using a combination of timeliness and accuracy

if (isMoreAccurate) {

return true;

} else if (isNewer && !isLessAccurate) {

return true;

} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {

return true;

}

return false;

}

/** Checks whether two providers are the same */

private boolean isSameProvider(String provider1, String provider2) {

if (provider1 &#61;&#61; null) {

return provider2 &#61;&#61; null;

}

return provider1.equals(provider2);

}

我的应用程序

import android.app.Application;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

import retrofit2.Retrofit;

import retrofit2.converter.gson.GsonConverterFactory;

public class MyApplication extends Application {

private API api;

private OkHttpClient client;

private static MyApplication sInstance;

&#64;Override

public void onCreate() {

super.onCreate();

sInstance &#61; this;

configureAPI();

}

private void configureAPI() {

client &#61; new OkHttpClient.Builder()

.connectTimeout(80, TimeUnit.SECONDS)

.writeTimeout(300, TimeUnit.SECONDS)

.readTimeout(80, TimeUnit.SECONDS)

.build();

Retrofit retrofit &#61; new Retrofit.Builder()

.baseUrl(Server.API_URL)

.addConverterFactory(GsonConverterFactory.create())

.client(client)

.build();

api &#61; retrofit.create(API.class);

}

public API getAPI() {

return api;

}

public static MyApplication getInstance() {

return sInstance;

}

}

API

public interface API {

&#64;FormUrlEncoded

&#64;POST("updateLocation")

Call update(&#64;Query("token") String token, &#64;Query("lat") String latitude, &#64;Field("long") String longitude);

}

服务器

public class Server {

public static final String API_URL &#61; "http://192.168.146.2:8090/";

public static final String REG_API_URL &#61; "http://192.168.120.2:8090/";

public static final String SndMsg_API_URL &#61; "http://192.168.120.2:8090/";

}

主要活动

public class MainActivity extends AppCompatActivity {

&#64;Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Intent serviceIntent &#61; new Intent(getApplicationContext(), LocationUpdaterService.class);

startService(serviceIntent);

}

}



推荐阅读
author-avatar
wing96333
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有