我在我的设备上实现了一个简单的应用程序,可以获取并显示您的GPS经度和纬度。然而,即使我在我的建筑物中移动,坐标也没有改变。这是我的实施:
foodActivity.java
public class foodActivity extends Activity {
LocationListener mlocListener;
Location location;
String provider;
private double latitude = 0;
private double longitude = 0;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_food);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.header_food);
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = mlocManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
location = mlocManager.getLastKnownLocation(provider);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(provider, 0, 0, mlocListener);
if (location != null) {
mHandler = new Handler();
mHandler.postDelayed(updateTask, 0);
} else {
Toast.makeText(getBaseContext(), "Location can;t be retrieved", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getBaseContext(), "No Provider found", Toast.LENGTH_SHORT).show();
}
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
TextView lats = (TextView) findViewById(R.id.tv_latitude);
TextView longs = (TextView) findViewById(R.id.tv_longitude);
latitude = loc.getLatitude();
longitude = loc.getLongitude();
lats.setText("Latitude :" + latitude);
longs.setText("Longitude :" + longitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_SHORT).show();
}
}
public Runnable updateTask = new Runnable(){
public void run(){
mlocListener.onLocationChanged(location);
mHandler.postDelayed(updateTask,50000);
}
};
}
activity_food.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.hkuapp.foodActivity">
<TextView
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_latitude"
android:layout_below="@+id/tv_longitude"
android:layout_alignParentLeft="true"
android:layout_marginTop="32dp" />
<TextView
android:text="Yo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_longitude"/>
</RelativeLayout>
答案 0 :(得分:0)
在此方法mlocManager.requestLocationUpdates(provider, 0, 0, mlocListener);
中,第二个参数是位置更新之间的时间间隔,以毫秒为单位。尝试将其更改为1000
(1秒)。
答案 1 :(得分:0)
从我在您的代码中看到的内容: