Android - Is there a way to listen if GPS was enabled or disabled

假如想象 提交于 2020-01-01 15:56:10

问题


I write this code to receive location updates -

PendingIntent launchIntent = PendingIntent.getBroadcast(context, 5000, intent, 0);
manager.requestLocationUpdates(selectProvider(), 0, 0, launchIntent);

I select the gps provider if its available, otherwise the network provider. Is there a way I could switch back to the gps one if it gets enabled later?

Does the system broadcast an intent when gps provider is enabled or disabled? Could I set up a listener for the same?


回答1:


public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_LONG).show();

}

public void onProviderDisabled(String provider) {
    Toast.makeText(this, "GPS disconnected", Toast.LENGTH_LONG);
        Toast.makeText(getApplicationContext(), "Connected",
                Toast.LENGTH_LONG);


    Toast.makeText(this, "Please Switch on GPS." + provider,
            Toast.LENGTH_LONG).show();
}



回答2:


You can detect if GPS status changed, e.g. if a GPS satellite fix was acquired.

Look at GpsStatus.Listener. Register it with locationManager.addGpsStatusListener(gpsStatusListener).



来源:https://stackoverflow.com/questions/7521509/android-is-there-a-way-to-listen-if-gps-was-enabled-or-disabled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!