首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我怎样才能使流程自动化?

我怎样才能使流程自动化?
EN

Stack Overflow用户
提问于 2014-01-17 04:06:07
回答 2查看 118关注 0票数 0

我做了一个GPS的教程,你可以按一个按钮获取位置,但现在我想让这个过程自动进行,我试着在onCreate方法上调用它,但只有效一次……知道怎么回事吗?这就是我所尝试的:

GPS类

代码语言:javascript
复制
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;
}

}

以及main-activity类:

代码语言:javascript
复制
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();
    }
}

先谢谢你...

EN

回答 2

Stack Overflow用户

发布于 2014-01-17 04:12:27

是否希望使用用户位置信息间歇性地更新EditText?如果是这样,您可以创建一个线程,该线程每隔一段时间调用一个检索位置数据的函数。

票数 0
EN

Stack Overflow用户

发布于 2014-01-17 05:07:11

如果只在MainActivity中使用GPS类,则将其作为您的活动的内部类,或者在您的Activity类本身中实现LocationListener,以更新onLocationChanged方法中的TextViews。

在使用之前进行locationManger null检查,在多次使用之后再检查null是没有意义的。

你的类结构应该是这样的

代码语言:javascript
复制
 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编辑器中编写了全部代码,所以如果发现任何语法错误,请进行更正。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21171762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档