开发者

Ensuring a strong GPS signal before enabling a button in Android

开发者 https://www.devze.com 2023-03-28 05:03 出处:网络
How can you ensure a good GPS signal is found before allowing a button click which starts logging GP开发者_Python百科S locations?Well, if you use the LocationManager-class and act upon the onLocationC

How can you ensure a good GPS signal is found before allowing a button click which starts logging GP开发者_Python百科S locations?


Well, if you use the LocationManager-class and act upon the onLocationChanged(), it won't actually trigger until it's gotten a position.

Edit: Here's something you can try out. Edit#2: D'oh, I actually misread the question. I'll leave my response though.

.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
  <Button android:id="@+id/mybutton1"
      android:visibility="INVISIBLE"
      android:layout_width="200dp"
      android:layout_height="100dp"
      android:text="MyButton1"
  />
</LinearLayout>

.java:

//Get the LocationManager & Button
final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final Button button = (Button) findViewById(R.id.mybutton1);

//Add the listener
LocationListener locationListener = new LocationListener() {

    public void onLocationChanged(Location location) {

        //Remove the listener and make the button visible        
        locManager.removeUpdates(locationListener);
        button.setVisibility(1);
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}
};

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

Let me know if this works out for you!


To check for a GPS fix, you'll have to register a gpsstatus.listener and look at the GPS status and the timing in between updates.

Check out this link for how: How can I check the current status of the GPS receiver?


You can use this code inside your button click to check the status of GPS.

 LocationListener ll = new MyLocationListener();
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);  
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, ll);

  LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

      Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);


        if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

            Log.i("About GPS", "GPS is Enabled in your devide");
            AlertDialog.Builder GPSON =new Builder(MyCardio.this);
            GPSON.setCancelable(false);
            GPSON.setMessage("GPS IS ON");
            GPSON.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            });
            GPSON.show();           
        } else {

             AlertDialog.Builder  GPSOFF = new Builder(MyCardio.this);
             GPSOFF.setCancelable(false);
             GPSOFF.setTitle("Connection Error");
             GPSOFF.setMessage(" Please Enable Your GPS !");
             GPSOFF.setPositiveButton("Ok",new DialogInterface.OnClickListener() 
               {
                   public void onClick(DialogInterface dialog,int which) 
                   {


                     //  startActivity(new Intent(LoginActivity.this,MainMenuActivity.class));
                   }
               });
             GPSOFF.show();
                    }
0

精彩评论

暂无评论...
验证码 换一张
取 消