本文介绍了如何仅使用GPS在android中没有互联网的情况下获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想使用GPS获取位置.我不想在此应用程序中使用Internet和GPRS.我的代码如下;告诉我我在哪里错了.
I want to get location using GPS only. I don't want to use internet and GPRS in this application. My code is below; tell me where I'm wrong in this.
代码:
package com.getlocation;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class UseGps extends Activity {
/** Called when the activity is first created. */
private String provider;
LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); /*
* Use the LocationManager class to
* obtain GPS locations
*/
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 61000, 250,
mlocListener);
} /* Class My Location Listener */
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " + "Latitude = "
+ loc.getLatitude() + "Longitude = " + loc.getLongitude();
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
.show();
Log.d("TAG", "Starting..");
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}/* End of Class MyLocationListener */
}/* End of UseGps Activity */
推荐答案
仅将其用于GPS Provider,不需要GPRS.
Use this for only GPS Provider, it does not need GPRS.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
您需要将许可权放入清单文件中.
You need to put the permission in manifest file.
这篇关于如何仅使用GPS在android中没有互联网的情况下获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!