0

我很难找到一个错误,我显然应该在某个地方。可能是我不知道的逻辑错误。

我有一个按钮,点击后会启动一个 locationManager 来定位我的位置。在扫描我的位置时,我希望禁用我的按钮。我设法做到了(耶),尽管在第一次检索位置并第二次单击按钮后,为了第二次接收位置,该按钮不再被禁用。这使我能够多次点击按钮,启动越来越多的 locationListeners 监听位置。这正是我试图通过在扫描时禁用按钮来防止的。

这是我的代码。解释如下。

主要活动:

public class MainActivity extends FragmentActivity {

// Setting layout dependent variables
...
private Button btn_scan;

// Setting several variables
...

// Setting default values
...

private boolean locationReceived = false;
private boolean gsmReceived = false;
private boolean isBusy = false;

private int counter = 0;
private Handler handler;
private Runnable runnable = new Runnable() {

    public void run() {

        counter ++;
        handler.postDelayed(runnable, 1000);

        if (locationReceived && gsmReceived) {

            // SAVE DATA TO DB HERE! //
            displayInformation();
            handler.removeCallbacks(runnable);

        } else if (counter >= (TIME_TO_WAIT_FOR_RESPONSE+1)) {

            displayInformation();
            handler.removeCallbacks(runnable);

        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Application context
    context = getApplicationContext();

    // Handler
    handler = new Handler();

    // initializing layout dependent variables
    ...
    btn_scan = (Button) findViewById(R.id.btn_scan);

    // Initialize GoogleMap
    ...

    // Get the LocationManager for checking, if providers are enabled
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    // Get the PhoneStateListener and telephony service to receive the signal strength
    myListener = new MyPhoneStateListener();
    tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    // Ask for the current location in here.
    locationResult = new LocationResult() {

        @Override
        public void gotLocation(final Location location) {

            // If location given, do something with it. If not, return some message.
            if (location != null) {

                updateGeoData(location);

            }

        }
    };

    btn_scan.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            if (!isBusy) {
                toogleButton();

                // if GPS is enabled in phone settings
                if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

                    postGPSAlert();
                    toogleButton();

                } else {

                    // If GPS enabled, get info
                    getInformation();
                }
            }
        }
    });
}

/*
 * wrapper method for getting informations
 */
private void getInformation() {

    // start informationfetcher. 
    runnable.run();
    // get geo location, longitude & latitude
    getGeoLocation();
    // get signal strength and its' other parameters
    getSignalStrength();
    // get internet connectivity quality string
    getConnectivityQuality();
    // get internet connectivity type
    getConnectivityType();
}

/*
 * get signal strength. only starts the listener, which does the rest of the job.
 */
public void getSignalStrength() {

    tel.listen(myListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}

/*
 * get connectivity type and update the variable internetType
 */
private void getConnectivityType() {

   ... nothing important
}

/*
 * Gets internet quality string
 */
private void getConnectivityQuality() {

    ... nothing important
}

/*
 * Gets the geo location
 */
public void getGeoLocation() {

    MyLocation myLocation = new MyLocation(context, TIME_TO_WAIT_FOR_RESPONSE);
    if (!myLocation.getLocation(locationResult)) {
        String message = "To make use of localization, please enable GPS or mobile networking.";
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }
}

/*
 * Updates retrieved GeoData
 */
private void updateGeoData(Location location) {

    latitude = location.getLatitude();
    longitude = location.getLongitude();    
    locationReceived = true;
}

/*
 * Updates GUI with all received values
 */
private void displayInformation() {

    ... only updating textViews here

    if (gsmReceived) {

        ... only updating textViews here

    }

    if (locationReceived) {

        ... only updating textViews here and moving googleMapCamera to some spot

    } 

    // Post Error messages, if location and/or GSM could not be obtained.
    ... just Toasting some error messages, if no signal or GPS received.

    toogleButton();
}

/*
 * Posts an alert saying u gotta enable GPS
 */
private void postGPSAlert() {
    // Prompt alert
    AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

    // Title and message values
    alert.setTitle(getString(R.string.prompt_titleProviderFail));
    alert.setMessage(getString(R.string.prompt_messageProviderFail));

    // If OK was clicked, redirect to settings to enable GPS
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {

            startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }
    });

    // If ABORT was clicked, redirect back to activity
    alert.setNegativeButton("Abort", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {

            // Canceled.
        }
    });

    // Show dialog
    alert.show();
}

/*
 * Method for disabling and enabling the button on pressure/update
 */
private void toogleButton() {

    if (btn_scan.isEnabled()) {

        isBusy = true;
        btn_scan.setText("SCANNING ...");
        btn_scan.setEnabled(false);
    } else if (!btn_scan.isEnabled()) {

        isBusy = false;
        btn_scan.setText("SCAN");
        btn_scan.setEnabled(true);
    }
}

/*
 * (non-Javadoc)
 * Listener, to listen on signalStrength of your phone. It'll set all the variables declared above and update the info you'll see.
 */
private class MyPhoneStateListener extends PhoneStateListener {

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {

        super.onSignalStrengthsChanged(signalStrength);

        ... just updating some String variables here

        gsmReceived = true;

        tel.listen(myListener, PhoneStateListener.LISTEN_NONE);
    }
}  

}

现在解释一下:一旦我单击按钮,我就会检查我是否已经在运行一个操作,并且我正在通过方法toogleButton()禁用该按钮。因此,触发了几种方法来收集有关网络、信号强度和我的位置的一些信息。收集我的位置通常需要大部分时间,但这并不重要。如果收集了所有信息,我正在使用处理程序+runnable 每秒检查 60 秒。如果是这样,我将通过UserInterface 上的displayInformation()显示所有信息,并通过toogleButton()再次启用该按钮。另外,我正在设置变量isBusy = true. 如果我没有收集所有信息并且 60 秒过去了,我仍然会显示所有(直到那时)收集的信息并再次切换按钮(到启用状态)。

所有这一切都很好,这是第一次。当我在收到所有信息后第二次按下按钮时,该按钮不再被禁用,尽管该应用程序仍会按应有的方式收集信息。这里唯一的问题是我现在可以多次按下按钮并启动多个请求来收集信息。那不是我想要的。我要一张一张

你知道我在这里做错了什么吗?

4

2 回答 2

1

Use Asynctask thread - disable button in onPreExecute , write location search code inside doInBackground() & enable button inside onPostExecude.

于 2013-07-06T10:26:32.373 回答
1

你的代码有点乱。您不应更改 toggleButton 方法中的 isBusy 字段。删除这些部分并将它们放在您知道定位过程开始和结束的地方。我认为 Button.isEnabled() 和 isBusy 之间发生冲突导致了这个逻辑错误。

于 2013-07-06T11:12:19.877 回答