在Android中启用GPS

时间:2015-05-07 14:08:46

标签: android gps

我在我的应用中使用GPS位置,但是当GPS未启用应用程序崩溃时。 因为我是android develpment的新手,我用Google搜索了这个,我在Stackoverflow中找到了这个答案 Check if gps is on In Kitkat (4.4)

这里的问题是PackageUtil& ACCESS_FINE_LOCATION未定义,我不确定我应该下载哪个库并嵌入到我的应用中

有什么建议吗?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="thumbnail_div" class="row">
  <input type="text" class="thumbnail" value="foo">
  <input type="checkbox" class="remove_thumbnail"> Remove Thumbnail
</div>

2 个答案:

答案 0 :(得分:1)

要在Android中设置权限,您必须在AndroidManifest.xml中执行此操作

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

应在<application>标记结束后声明。

docs

中详细了解相关信息

<强>更新

您可以像这样检查gps是否启用

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
    Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
}else{
    Toast.makeText(this, "GPS is Disabled in your devide", Toast.LENGTH_SHORT).show();
}

要显示GPS设置页面,您可以使用

Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);

答案 1 :(得分:1)

首先在清单

中添加此权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

删除您编写的if代码。

然后添加此GPS检查器代码。此代码检查GPS是否启用。如果未启用,则会打开GPS设置。

private void CheckEnableGPS() {
        String provider = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if(!provider.equals("")){
            //GPS Enabled
        }else{
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Enable GPS");  // GPS not found
            builder.setMessage("The app needs GPS to be enabled do you want to enable it in the settings? "); // Want to enable?
            builder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                }
            });
            builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                }
            });
            builder.setCancelable(false);
            builder.create().show();
            return;
        }
    }