定期获取网络提供商更新

时间:2013-09-13 22:02:40

标签: android

我的目标是在某些时间间隔(x秒)内使用requestlocationupdate()方法定期从网络提供商处获取位置更新,然后执行LocationManager.removeUpdates()方法以删除更新并尽可能降低功耗,

面对我的问题是调用了requestlocationupdate()方法并且每45秒调用一次onlocationchanged方法,即使它的“mintime”(此方法中的第二个参数)小于45 !!为什么会发生这种情况?

另外,第二个问题是在每个间隔期间,我需要调用requestlocationupdates方法并记录用户位置的当前纬度和经度,并将其与记录的最后一个位置的纬度和经度进行比较,如果它相同(用户仍然静止),显示“您的最后位置是......”,如果位置发生变化,则显示“您的更新位置为....”。
我怎么能这样做?

我尝试使用handler.post(myRunnable)解决这个问题,并使用timertask类调用runnable对象,每隔(x秒)执行requestlocationupdates()方法,该方法在timertask输入参数中确定。

当我测试这段代码时,它每隔x秒获得经度和经度,但即使我仍然在同一位置,它的记录值也会发生变化!

另外,正在编写onlocationchanged()覆盖方法中的任何代码代表了节能方面的好方法吗?

另外,你能告诉我如何获得任何网络提供商(wifi / 3G)的准确性吗?

非常感谢任何帮助!

代码如下:

intializations:

Timer t1;
Timer t2;
TimerTask mTimerTask1;
TimerTask  mTimerTask2;
Handler hand;
Handler hand1;
public LocationManager locationManager;
// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
double Altitude; // longitude
double accuracy; // accuracy


// The minimum distance to change notifications Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 0 meters

// The minimum time between notifications updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 10; // 10 sec 

处理程序部分:

 Runnable run1 = new Runnable() {
    public void run() {

        mTimerTask1 = new TimerTask() {
            public void run() {
                hand.post(new Runnable() {
                    public void run() {
                        if(check_conectivity()){
                            //flag1=false;
                            get_location();
                            time++;
                        }

                    }
                });

            }
        };
t1.scheduleAtFixedRate(mTimerTask1, 0, 10000);

    }
};

check_connectivity方法:

public boolean check_conectivity(){


    // put the reference LocationManger
    locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
    // getting network status
    // isProviderEnabled ==> Returns the current enabled/disabled status of the NETWORK_PROVIDERr.
    //  If the user has enabled this provider in the Settings menu, true is returned otherwise false is returned
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if ( !isNetworkEnabled) {
        // no network provider is enabled
        showSettingsAlert();
        return false;

    } 
    else{

        return true;
    }

}

get_location方法:

public void get_location(){

                    if (isNetworkEnabled) {

    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    Log.d("Network", "Network");

onlocationchanged()覆盖方法:

if (locationManager!= null){

    location = locationManager
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    if (location != null) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();

        tv2.setText("Your last Location is - \nLat: " + latitude + "\nLong: " + longitude );
        tv3.setText("Last estimeted location number : " + time1);
        time1++;
        stopUpdating();

1 个答案:

答案 0 :(得分:0)

  

你能告诉我如何获得任何网络提供商(wifi / 3G)的准确性

据我所知,你做不到。但您可以过滤回复并告诉设备仅在准确性为.....时才会通知您。

第一关,您可以使用postDelayed代替post

private int mSampleDurationTime = 10000; // 10 sec for example 
...    
mHandler.postDelayed(mRunnable, mSampleDurationTime);`

第二,你可以尝试在我的一个答案中描述“最佳位置”,请参阅链接here

希望它会对你有所帮助,