简体   繁体   中英

Waiting for GPS location before proceeding Android

I am aware there are a number of similar questions floating around but could not find a suitable answer in my searches.

My App has an activity which relies on the GPS location to function. The app will crash if it is run immediately after start up due to null pointer exceptions etc where the location is null. I am aware these are my programming errors and I should handle the exceptions, but the activity is still useless until a GPS fix is found.

When the app crashes I get the "Searching for GPS" icon, and if this is left for long enough a GPS location is found and the app can be run with no issue.

I have created another activity to act as a home screen which will enable the button to link to the above activity when a GPS location is found. The problem is that it doesn't seem to find or be looking for a GPS signal. By this i mean that no "Searching for GPS" icon appears and the button is never enabled. The code for this home activity is below, have I missed something?

My activity implements LocationListener, and the code below is in the Activity "onCreate" method. "startExplore" starts my GPS reliant activity.

    exploreButton = (Button) findViewById(R.id.button1);

    exploreButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startExplore();
        }
    });

    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the location provider -> use
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
      System.out.println("Provider " + provider + " has been selected.");
      onLocationChanged(location);
    } else {
        exploreButton.setEnabled(false);
        exploreButton.setText("Finding location . . .");
    }

This is my "onLocationChanged" method. As i said above, unlike my other activity I do not get the "Searching for GPS" icon and the location does not appear to be found despite this being the same method used in my other Activity. So is this the correct technique for waiting until a location is found, and can anyone identify any mistakes?

public void onLocationChanged(Location location) {
    exploreButton.setEnabled(true);
    exploreButton.setText("Found signal");

}

调用locationManager.requestLocationUpdates(locationProvider, 0, 0, this)以获取更新的位置。。这可能是问题的根源。由于这个原因,我认为没有启动“仅搜索GPS”。

Move it out the onCreate methode or put it in a handler or so.

Example:

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
          //methods you want.
      }
    }, 100);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM