是否有任何一点毛病我越来越位置的方法呢? [英] Is there any thing wrong with my getting location approach?
本文介绍了是否有任何一点毛病我越来越位置的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要提交我的小项目。我写一个类来获取用户的位置。我想知道,如果在我的做法有任何技术问题。它工作正常。我使用的处理程序,不断地位置。
I have to submit my mini project. I write a class to get the user location. I want to know if there is any technical problem in my approach or not. It works fine. I am Using a Handler to get the location continuously.
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) {
}
}
`
推荐答案
是的,在我看来,两件事情是错误的。
Yeah, As I see it, 2 things are wrong.
- 请不要检查可用性
requestLocationUpdates
。 - 不听数据连接的变化和
removeUpdates
和requestLocationUpdates
。
- Don't check for availability to
requestLocationUpdates
. - Instead listen to DATA connection changes and
removeUpdates
, andrequestLocationUpdates
.
样code
// OnCreate中
//OnCreate
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(new NetworkConnectionState(this.getApplicationContext()),
PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
| PhoneStateListener.LISTEN_CELL_LOCATION );
//你的听众。
//Your listener
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 {
}
}
编辑:
不要忘记上更新连接/断开 onProviderDisabled
和 onProviderEnabled
在LocationListeners
Edit :
Dont forget to update your connection / disconnection on onProviderDisabled
and onProviderEnabled
in your LocationListeners
这篇关于是否有任何一点毛病我越来越位置的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文