如何提示用户启用 gps [英] How to prompt user to enable gps

查看:27
本文介绍了如何提示用户启用 gps的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提示用户启用 gps,我尝试了很多次来解决问题但无法清除它.我是 android 新手,任何人都可以帮助我........我的 mainactivity.java,

I am trying to prompt the user to enable gps, i tried many times to resolve issues but not able to clear it.I am new to android can any one please help me........ My mainactivity.java,

public class MainActivity extends AppCompatActivity {
private WebView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView.setWebContentsDebuggingEnabled(true);
    WebView view = (WebView) this.findViewById(R.id.webView);
    view.loadUrl("https://google.com");
    view.getSettings().setJavaScriptEnabled(true);
    view.getSettings().setDomStorageEnabled(true);
    view.getSettings().setDatabaseEnabled(true);
    view.getSettings().setAppCacheEnabled(true);
    view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    view.getSettings().setUseWideViewPort(true);
    view.getSettings().setLoadWithOverviewMode(true);
    view.getSettings().setBuiltInZoomControls(false);
    view.getSettings().setAllowUniversalAccessFromFileURLs(true);
    view.getSettings().setAllowContentAccess(true);
    view.setWebChromeClient(new WebChromeClient());
    view.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    view.setScrollbarFadingEnabled(false);
    view.getSettings().setBuiltInZoomControls(true);
    view.getSettings().setPluginState(PluginState.ON);
    view.getSettings().setAllowFileAccess(true);
    view.getSettings().setSupportZoom(true);
    CheckEnableGPS();
}
private void CheckEnableGPS(){
    String provider = Settings.Secure.getString(getContentResolver(),
            Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(!provider.equals("")){
        //GPS Enabled
        Toast.makeText(AndroidEnableGPS.this, "GPS Enabled: " + provider,
                Toast.LENGTH_LONG).show();
       }else  {
          Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
          startActivity(intent);
       }
     }
    }

我的 androidmanifest.xml,

my androidmanifest.xml,

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.test">
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  <uses-permission android:name="android.permission.CAMERA"/>
  <uses-feature android:name="android.hardware.camera" />
  <uses-permission android:name="android.permission.ACCESS_GPS" />
  <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS"   />
  <uses-permission android:name="android.permission.ACCESS_LOCATION" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.BLUETOOTH"/>
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

推荐答案

实现 LocationListener :

Implement LocationListener :

@Override
public void onLocationChanged(Location location) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider)
{
    if (provider.equals(LocationManager.GPS_PROVIDER))
    {
        showGPSDiabledDialog();
    }
}     

显示对话框以打开设置:

Show dialog to open Settings:

public void showGPSDiabledDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("GPS Disabled");
    builder.setMessage("Gps is disabled, in order to use the application properly you need to enable GPS of your device");
    builder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), GPS_ENABLE_REQUEST);
        }
    }).setNegativeButton("No, Just Exit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    mGPSDialog = builder.create();
    mGPSDialog.show();
}

在你的 onCreate 方法调用中:

In your onCreate Method call :

LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    return;
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

并重写 onActivityResult 方法来测试用户是否正确激活了 GPS.

And override onActivityResult method to test if the user activated GPS correctly.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == GPS_ENABLE_REQUEST) {
        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        }

        if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            showGPSDiabledDialog();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

这篇关于如何提示用户启用 gps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆