我制作一个GPS教程,你可以按下按钮获得位置,但现在我想自动制作这个过程,我尝试在onCreate方法上调用它但只能工作一次....任何想法如何?这是我尝试的:
GPS CLASS
public class GPS implements LocationListener{
private final Context mContext;
//flag for GPS status
boolean isGPSEnable = false;
// Flag for network status
boolean isNetworkEnable = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; //location
double latitude; //latitude
double longitude; //longitude
// the minimum distances to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // Minimun distance 10 meters
// The minimum time between updates in millisenconds
private static final long MIN_TIME_BTW_UPDATES = 1000 * 60 * 1; // 1 MINUTE
// Declaring a Location Manager
protected LocationManager locationManager;
public GPS(Context context){
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);
//getting GPS status
isGPSEnable = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
//getting network status
isNetworkEnable = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnable && !isNetworkEnable){
// no network provider is enable
}
else {
this.canGetLocation = true;
if(isNetworkEnable){
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BTW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this);
Log.d("Network", "Network");
if(locationManager != null){
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enable get lat/long using GPS Services
if(isGPSEnable){
if(location == null){
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BTW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS ENABLE", "GPS Enabled");
if(locationManager != null){
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
*/
public void stopUsingGPS(){
if (locationManager != null){
locationManager.removeUpdates((LocationListener) GPS.this);
}
}
/**
* function to get latitude
*/
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
//return latitude
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
//return longitude
return longitude;
}
/**
* Function to check GPS/wifi enable
* @return boolean
*/
public boolean canGetLocation(){
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will launch Settings Options
*/
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle(mContext.getString(R.string.AlertDialog_Tittle));
// Setting Dialog Message
alertDialog.setMessage(mContext.getString(R.string.dialog_message));
// On pressing Setting button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
}
});
// On pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
@Override
public void onLocationChanged(Location location){
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
和主要活动类:
public class MainActivity extends ActionBarActivity {
private EditText edTLatitud;
private EditText edTLongitud;
private EditText edTCompass;
private EditText edTDirecc;
private SensorManager sensorManager;
private Sensor compassSensor;
// GPS class
GPS gps;
// Compass class
Compass compass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gpsactivity);
edTCompass = (EditText)this.findViewById(R.id.edTxtBrujula);
edTDirecc = (EditText)this.findViewById(R.id.edTxtBrujdireccion);
edTLatitud = (EditText)this.findViewById(R.id.edTxtLatitud);
edTLongitud = (EditText)this.findViewById(R.id.edTxtLongitud);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
compassSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
gps = new GPS(MainActivity.this);
// Check if GPS is enable
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// Print on the screen the coordinates
UpdateGPSonScreen(latitude, longitude);
}
else {
// can't get location
// GPS or network is not enable
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
// Show the Latitude and longitude of the GPS on the application
public void UpdateGPSonScreen(double latitude, double longitude)
{
try{
edTLatitud.setText(String.valueOf(latitude));
edTLongitud.setText(String.valueOf(longitude));
}
catch (Exception e)
{
e.printStackTrace();
}
}
提前感谢...
答案 0 :(得分:0)
您是否希望间歇性地使用用户位置信息更新EditText?如果是这样,您可以创建一个间隔调用检索位置数据的函数的线程。
答案 1 :(得分:0)
如果除了MainActivity之外的其他地方没有使用GPS类,请将其作为Activity的内部类或在Activity类本身中实现LocationListener,以更新onLocationChanged
方法中的TextViews。
在任何地方使用它之前先进行locationManger
null检查,在多次使用它之后检查无效是没有意义的。
您的班级结构应该像
public class MainActivity extends ActionBarActivity{
// Define your text views to use them globally
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gpsactivity);
//find all your views
}
class GPS implements LocationListener{
//copy all your code here with required correction
@Override
public void onLocationChanged(Location location){
//get your latitude and longitude from location Object
// call same method to update your view
}
}
}
我只在stackoverflow编辑器中编写了整个代码,因此如果发现任何语法错误,请进行更正。