【问题标题】:Is there any thing wrong with my getting location approach?我的获取位置方法有什么问题吗?
【发布时间】:2013-02-05 18:39:50
【问题描述】:

我必须提交我的迷你项目。我写了一个类来获取用户位置。我想知道我的方法是否存在任何技术问题。它工作正常。我正在使用处理程序连续获取位置。

Main.java

public class Main extends Activity {

LocationTracker lt;
TextView tv;
Handler handler = new Handler();
Runnable locationRunner = new Runnable() {

    @Override
    public void run() {
        if (lt.canGetLocation()) {
            tv.setText(lt.getLatitude() + " " + lt.getLongitude());
        }
        handler.postDelayed(this, 1000);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lt = new LocationTracker(this);
    Toast.makeText(
            this,
            "GPS: " + lt.isGPSEnabled() + "\nNetwork: "
                    + lt.isNetworkEnabled() + "\nCanGetLocation: "
                    + lt.canGetLocation(), Toast.LENGTH_LONG).show();
    tv = (TextView) findViewById(R.id.textView1);
}

@Override
protected void onResume() {
    super.onResume();
    handler.postDelayed(locationRunner, 1000);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    handler.removeCallbacks(locationRunner);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

LocationTracker.java

public class LocationTracker implements LocationListener {

private Context context;
private boolean isGPSEnabled;
private boolean isNetworkEnabled;
private boolean isTracking;
private Location location;
private LocationManager locationManager;

public LocationTracker(Context context) {
    this.context = context;
    locationManager = (LocationManager) context
            .getSystemService(context.LOCATION_SERVICE);
    isNetworkEnabled = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    isGPSEnabled = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (!isNetworkEnabled && !isGPSEnabled) {
        isTracking = false;
    } else {
        isTracking = true;
    }

    if (isGPSEnabled) {
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, this);
    } else if (isNetworkEnabled) {
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, this);
    }

}

public double getLatitude() {
    if (location != null) {
        return location.getLatitude();
    } else {
        return 0.00;
    }
}

public double getLongitude() {
    if (location != null) {
        return location.getLongitude();
    } else
        return 0.00;
}

public boolean canGetLocation() {
    return isTracking;
}

public boolean isGPSEnabled() {
    return isGPSEnabled;
}

public boolean isNetworkEnabled() {
    return isNetworkEnabled;
}

@Override
public void onLocationChanged(Location arg0) {
    location = arg0;
}

@Override
public void onProviderDisabled(String arg0) {
}

@Override
public void onProviderEnabled(String arg0) {
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

}

}

`

【问题讨论】:

    标签: android


    【解决方案1】:

    是的,在我看来,有两件事是错误的。

    1. 不要检查requestLocationUpdates 的可用性。
    2. 改为监听 DATA 连接更改和removeUpdatesrequestLocationUpdates

    示例代码

    //OnCreate

    tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(new NetworkConnectionState(this.getApplicationContext()),
                PhoneStateListener.LISTEN_DATA_CONNECTION_STATE 
    | PhoneStateListener.LISTEN_CELL_LOCATION );
    

    //你的听众

    public void onDataConnectionStateChanged(int state, int networkType) {
            // We have changed protocols, for example we have gone from HSDPA to
            // GPRS
            // HSDPA is an example of a 3G connection
            // GPRS is an example of a 2G connection
            if (state == TelephonyManager.DATA_CONNECTED) {
            }
            else {
            }
        }
    

    编辑: 不要忘记在 LocationListeners 中更新 onProviderDisabledonProviderEnabled 的连接/断开连接

    【讨论】:

    • 对不起,先生,但为什么我不必检查可用性?我正在听 requestLocationUpdates() 。你能解释更多吗?我没有得到你:-)
    • 你的意思是 onDestroy() 被调用并且我停止监听 updateListener ?好的,我明白了
    • 你只是检查一次可用性,而不是这个网络非常不稳定,你需要知道网络何时可用,何时不可用。您希望系统发送一个事件/回调让您对其进行处理。这是一个更好的设计。
    • 不仅在 onDestroy 上,而且在 "DATA_CONNECTED" = false 上。希望它清楚。
    【解决方案2】:

    isTraking=false 如果在应用启动时 gps 和网络都被禁用,它永远不会更改为 true,即使用户在应用运行期间启用了 gps 或网络。您应该使用 onProviderDisabled 和 onProviderEnabled 方法中的逻辑。

    【讨论】:

    • 在我获得位置之前,我正在检查一个boolean 变量,只有当两个提供者之一启用时,该变量才会为真。像 This public void run() { if (lt.canGetLocation()) { tv.setText(lt.getLatitude() + " " + lt.getLongitude()); } handler.postDelayed(this, 1000); } 但是是的,在我的应用程序运行期间,我可以根据情况操纵这个标志是真还是假
    • o 是的。我明白了@jason Yong。谢谢。对我有好处:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    相关资源
    最近更新 更多