MediaPad 10FHD 爲什麼不可以室內WiFi定位

import android.app.AlertDialog;android

import android.app.Service;git

import android.content.DialogInterface;網絡

import android.content.DialogInterface.OnClickListener;app

import android.content.Intent;less

import android.location.Location;ide

import android.location.LocationListener;函數

import android.location.LocationManager;ui

import android.location.LocationProvider;this

import android.os.Bundle;spa

import android.os.IBinder;

import android.util.Log;

import android.widget.Toast;


import com.ecity.loactionbynetandgps.common.Common;


/**

 * @author fjm

 * 

 */

public class LocationSvc extends Service {


private static final String TAG = "LocationSvc";

private LocationManager locationManager;

private MyLocationListner gpsListener;

private MyLocationListner networkListener;


@Override

public IBinder onBind(Intent intent) {

return null;

}


@Override

public void onCreate() {

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

gpsListener = new MyLocationListner();

networkListener = new MyLocationListner();

}


@Override

public void onStart(Intent intent, int startId) {

boolean isEnable = false;

if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

       AlertDialog builder = new AlertDialog.Builder(getApplicationContext())

               .setMessage("Enable Network Connection")

               .setTitle("Warning!")

               .setCancelable(true)

               .setPositiveButton("OK", new OnClickListener() {

                   @Override

                   public void onClick(DialogInterface dialog, int which) {

                       return;

                   }

               } ).show();

   }

if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {

locationManager.requestLocationUpdates(

LocationManager.NETWORK_PROVIDER, 2000, 0, networkListener);

isEnable = true;

}

if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {

locationManager.requestLocationUpdates(

LocationManager.GPS_PROVIDER, 1000 * 2, 50, gpsListener);

isEnable = true;

}

if (!isEnable)

Toast.makeText(this, "沒法定位", Toast.LENGTH_SHORT).show();

}


@Override

public boolean stopService(Intent name) {

locationManager.removeUpdates(gpsListener);

locationManager.removeUpdates(networkListener);

return super.stopService(name);

}


// GPS和移動網絡的監聽器,回調函數

private class MyLocationListner implements LocationListener {

@Override

public void onLocationChanged(Location location) {

// Called when a new location is found by the location provider.

Log.v("GPSTEST",

"Got New Location of provider:" + location.getProvider());

if (currentLocation != null) {

if (isBetterLocation(location, currentLocation)) {

Log.v("GPSTEST", "It's a better location");

currentLocation = location;

showLocation(location);

} else {

Log.v("GPSTEST", "Not very good!");

}

} else {


Log.v("GPSTEST", "It's first location");

currentLocation = location;

showLocation(location);

}

/*// 移除基於LocationManager.NETWORK_PROVIDER的監聽器

if (LocationManager.NETWORK_PROVIDER.equals(location.getProvider())) {

locationManager.removeUpdates(this);

}*/


// 通知Activity

Intent intent = new Intent();

intent.setAction(Common.LOCATION_ACTION);

intent.putExtra(Common.LOCATION, location.toString());

sendBroadcast(intent);


System.out.println(location.toString());

// 若是隻是須要定位一次,這裏就移除監聽,停掉服務。若是要進行實時定位,能夠在退出應用或者其餘時刻停掉定位服務。

// locationManager.removeUpdates(this);

// stopSelf();

}


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

// 添加移除的網絡監聽器

if (LocationProvider.OUT_OF_SERVICE == status) {

Log.v("GPSTEST", "GPS服務丟失,切換至網絡定位");

locationManager

.requestLocationUpdates(

LocationManager.NETWORK_PROVIDER, 0, 0,

networkListener);

}

}


public void onProviderEnabled(String provider) {

}


public void onProviderDisabled(String provider) {

}

};


private Location currentLocation;


// 打印位置作標信息

private void showLocation(Location location) {

// 緯度

Log.v("GPSTEST", "Latitude:" + location.getLatitude());

// 經度

Log.v("GPSTEST", "Longitude:" + location.getLongitude());

// 精確度

Log.v("GPSTEST", "Accuracy:" + location.getAccuracy());

// Location還有其它屬性,請自行探索

System.out.println(location.toString());

}


// 判斷網絡和GPS定位精度,哪一個更高

private static final int CHECK_INTERVAL = 1000 * 30;


protected boolean isBetterLocation(Location location,

Location currentBestLocation) {

// 當前定位爲空,直接返回

if (currentBestLocation == null) {


return true;

}


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

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

boolean isSignificantlyNewer = timeDelta > CHECK_INTERVAL;

boolean isSignificantlyOlder = timeDelta < -CHECK_INTERVAL;

boolean isNewer = timeDelta > 0;


// If it'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 = (int) (location.getAccuracy() - currentBestLocation

.getAccuracy());

boolean isLessAccurate = accuracyDelta > 0;

boolean isMoreAccurate = accuracyDelta < 0;

boolean isSignificantlyLessAccurate = accuracyDelta > 200;


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

boolean isFromSameProvider = 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 == null) {

return provider2 == null;

}

return provider1.equals(provider2);

}


}

相關文章
相關標籤/搜索