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, GPS_REQUEST_CODE);
								}
							})

					.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;
	}

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


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值