检查用户是否在android中的设置中禁用GPS

时间:2016-03-29 19:50:54

标签: android

有任何解决方案可以检查用户是否在设置中禁用GPS? 我打开我的应用程序,打开Android系统的顶部工具栏,禁用GPS并关闭此工具栏。在这一刻,我想要检查GPS的状态是否已更改。 我在onResume()中使用检查GPS是否处于活动状态,但此解决方案仅在用户启用GPS时有效,此时禁用onResume()。 有什么想法吗?

编辑: 这可能是以下类:

public class PrivacyActivity extends BaseActivity  implements GpsStatus.Listener, LocationListener{


   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_privacy);

   }

   @Override
   protected void onPause() {
      super.onPause();
   }

   @Override
   protected void onResume() {
      super.onResume();
   }

   @Override
   public void onGpsStatusChanged(int event) {
      switch (event) {
         case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
            Toast.makeText(this, "ads", Toast.LENGTH_LONG).show();

            break;
         case GpsStatus.GPS_EVENT_FIRST_FIX:
            Toast.makeText(this, "ads1", Toast.LENGTH_LONG).show();

            break;
      }
   }

   @Override
   public void onLocationChanged(Location location) {

   }

   @Override
   public void onStatusChanged(String provider, int status, Bundle extras) {
      Toast.makeText(getApplicationContext(), "ads2", Toast.LENGTH_LONG).show();
   }

   @Override
   public void onProviderEnabled(String provider) {
      Toast.makeText(getApplicationContext(), "ads2", Toast.LENGTH_LONG).show();
   }

   @Override
   public void onProviderDisabled(String provider) {
      Toast.makeText(getApplicationContext(), "ads2", Toast.LENGTH_LONG).show();
   }
}

当我禁用gps时,我没有看到吐司。

2 个答案:

答案 0 :(得分:2)

您可以使用LocationListener类

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      makeUseOfNewLocation(location);
    }

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

    public void onProviderEnabled(String provider) {
        Log.i("Example", "GPS is ON");
    }

    public void onProviderDisabled(String provider) {
        Log.i("Example", "GPS is OFF");
    }
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

您可以在http://developer.android.com/guide/topics/location/strategies.html

中获取更多信息

答案 1 :(得分:0)

首先引用locationListener。

LocationListener locationListener;

locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            LatLng myPlace = new LatLng(location.getLatitude(), location.getLongitude());
            mMap.addMarker(new MarkerOptions().position(myPlace).title("me"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(myPlace));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(8.0f));
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    };
如果用户已从设置中禁用GPS,则会触发

onProviderDisabled方法。 方法内部的意图将使用户直接进行位置设置。

经过测试并且工作正常。