由于Android4.0以上不允许强制修改系统设置,所以要引导用于前往设置GPS功能启动。
启动GPS设置方法:
public static final int LOCATION_REQUEST = 110;
public static LocationManager openGPSSettings(final Context context){
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
DialogUtil.showDialog(context, "提示", "请先打开GPS开关", "确定", "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
((Activity)context).startActivityForResult(intent, LOCATION_REQUEST);
}
}, null);
}
return locationManager;
}
如何对是否打开做出响应呢:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == LOCATION_REQUEST){
if (mLocationManager != null && mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
loadWiFiList();
wifiAdapter.notifyDataSetChanged();
}
}
super.onActivityResult(requestCode, resultCode, data);
}