在android中调用gps按钮切换功能
我已经写了代码打开gps设置时,它是关闭的。但我需要在gps按钮设置为ON后调用一个函数。我该怎么做 当GPS按钮更改时,系统将发送广播通知该事件,因此 在在android中调用gps按钮切换功能,android,gps,Android,Gps,我已经写了代码打开gps设置时,它是关闭的。但我需要在gps按钮设置为ON后调用一个函数。我该怎么做 当GPS按钮更改时,系统将发送广播通知该事件,因此 在清单中添加权限和接收者,如下所示: <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> public class LocationChangeReceiver extends BroadcastReceiver { @Ov
清单
中添加权限
和接收者
,如下所示:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
public class LocationChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
LocationManager locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// gps enabled
} else {
// gps disabled
}
}
}
或者像这样使用onGpsStatusChanged
回调:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
public class LocationChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
LocationManager locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// gps enabled
} else {
// gps disabled
}
}
}
希望能有帮助