安卓系统-是否有办法监听GPS是否被启用或禁用?[英] Android - Is there a way to listen if GPS was enabled or disabled

本文是小编为大家收集整理的关于安卓系统-是否有办法监听GPS是否被启用或禁用?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我编写此代码以接收位置更新 -

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

i如果可用,请选择GPS提供商,否则网络提供商.如果以后启用,我有没有办法切换回GPS?

当GPS提供商启用或禁用时,系统是否会广播意图?我可以为此设置听众吗?

推荐答案

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();
}

其他推荐答案

您可以检测GPS状态是否更改,例如如果获取了GPS卫星修复.

查看 GpsStatus.Listener .将其注册为locationManager.addGpsStatusListener(gpsStatusListener).

本文地址:https://www.itbaoku.cn/post/102381.html

问题描述

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?

推荐答案

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();
}

其他推荐答案

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).