APP首次启动提示开启定位服务

APP首次启动提示开启定位服务

最近在项目中有用到位置,以前都是在进入需要位置信息的地方才去让用户打开定位,或者是在用户安装APP的时候就让用户给APP赋予读取位置的权限,但是现在想要用户在安装完App第一次启动的时候才去询问用户是否开启定位。

1.首先控制在第一次启动APP才会弹出,我们可以使用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);

}

}

这里如果是第一次登陆,SharedPreferences不会有存数据,此时isPopUpGPSTip为true.

2.在MainActivity去判断是否要弹出弹框提示

/**

* 第一次进入的要提示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;

}

如果用户已经是开启状态,那我们就不弹提示,如果不是,那就弹提示,最后的效果图如下:

————————————————

转载于:https://blog.csdn.net/nzzl54/article/details/78496488

相关发现

说卡卡10年世界杯后故意先度
完美体育365wm

说卡卡10年世界杯后故意先度

🌼 06-28 🌻 9803
名创大学
365bet365

名创大学

🌼 07-20 🌻 1131
淘宝怎么邀请下单?引导下单有何技巧?
兔子销售渠道有哪些(养兔场的兔子卖哪里?)