Home  >  Article  >  Web Front-end  >  How does uniapp determine whether mobile phone positioning is enabled?

How does uniapp determine whether mobile phone positioning is enabled?

coldplay.xixi
coldplay.xixiOriginal
2020-12-08 15:17:398453browse

The method for uniapp to determine whether mobile phone positioning is enabled: first obtain system information; then determine the platform, the code is [let system = uni.getSystemInfoSync();if (system.platform === 'android')].

How does uniapp determine whether mobile phone positioning is enabled?

The operating environment of this tutorial: windows7 system, uni-app2.5.1 version, Dell G3 computer.

Recommended (free): uni-app development tutorial

uniapp determines whether mobile phone positioning is turned on Method:

/**检查是否打开GPS功能(android)**/
export const checkOpenGPSServiceByAndroid = () => {
  let system = uni.getSystemInfoSync();// 获取系统信息
  if (system.platform === 'android') { // 判断平台
    var context = plus.android.importClass("android.content.Context");
    var locationManager = plus.android.importClass("android.location.LocationManager");
    var main = plus.android.runtimeMainActivity();
    var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
    if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
      uni.showModal({
        title: '提示',
        content: '请打开定位服务功能',
        showCancel: false, // 不显示取消按钮
        success() {
          if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
            var Intent = plus.android.importClass('android.content.Intent');
            var Settings = plus.android.importClass('android.provider.Settings');
            var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
            main.startActivity(intent); // 打开系统设置GPS服务页面
          } else {
            console.log('GPS功能已开启');
          }
        }
      });
    }
  } 
}

Related free learning recommendations: Programming Video

The above is the detailed content of How does uniapp determine whether mobile phone positioning is enabled?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn