Recientemente, la ubicación se ha utilizado en el proyecto. En el pasado, el usuario solo podía abrir la ubicación cuando ingresaba al lugar donde se requería la información de ubicación. Cuando la aplicación se instala por primera vez, le preguntará al usuario si habilitar el posicionamiento.
1. Primero, el control aparecerá cuando se inicie la aplicación por primera vez. Podemos usar SharedPreferences
public class CurrApplication extends BaseApplication{
public boolean isPopUpGPSTip;
//Leak Canary detect leak
// private RefWatcher mRefWatcher;
public static CurrApplication getInstance() {
return instance;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
instance = this;
firstPopUpGPSTipInit();
}
private void firstPopUpGPSTipInit() {
SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
isPopUpGPSTip = shared.getBoolean("isGPSTip", true);
}
}
Si este es el primer inicio de sesión, no habrá datos almacenados en SharedPreferences y isPopUpGPSTip es verdadero en este momento.
2. En MainActivity para determinar si aparece un aviso emergente
/**
* 第一次进入的要提示GPS定位
*/
private void needGPSTips(){
SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
if (CurrApplication.getInstance().isPopUpGPSTip) {
//第一次进入跳转
editor.putBoolean("isGPSTip", false);
editor.commit();
openGPSSettings();
}
}
/**
* 跳转GPS设置
*/
private void openGPSSettings() {
if (!checkGPSIsOpen()) {
//没有打开则弹出对话框
new AlertDialog.Builder(this)
.setTitle(R.string.notifyTitle)
.setMessage(R.string.gpsNotifyMsg)
// 拒绝, 退出应用
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton(R.string.setting,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//跳转GPS设置界面
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0);
}
})
.setCancelable(false)
.show();
}
}
/**
* 检测GPS是否打开
*
* @return
*/
private boolean checkGPSIsOpen() {
boolean isOpen;
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
isOpen = locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER);
return isOpen;
}
Si el usuario ya está en estado abierto, entonces no reproduciremos el indicador. Si no, lo reproduciremos. La representación final es la siguiente:
———————————————
Reimpreso en: https://blog.csdn.net/nzzl54/article/details/78496488