无法在 android api 23 上打开位置?

2023-12-13

我使用了以下代码,请求许可的对话按预期显示。但当我点击“允许”时,它什么也没做。日志消息不会显示为好像未授予权限,因此我转到参数来验证位置是否为“打开”以及“关闭”。难道不是因为我授予应用程序访问我的位置的权限而应该打开它吗? 如果我手动“打开”它,然后再次运行该应用程序,一旦它请求我的许可,它就会工作并显示日志消息,但这并不是请求权限(通过对话)来打开位置的全部意义(当它关闭)如果用户单击“允许”? 难道我做错了什么 ?我应该提到我正在 api23 上运行该应用程序

是我的代码Oncreate:

 mApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

 mApiClient.connect();

    // Create the LocationRequest object
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(10 * 1000)        // 10 seconds, in milliseconds
                .setFastestInterval(1 * 1000); // 1 second, in milliseconds

这是我的已连接 method:

    public void onConnected(@Nullable Bundle bundle) {
            //start the service
//checking and asking for permission

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            MY_PERMISSION_ACCESS_FINE_LOCATION);
                }
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            Location location = LocationServices.FusedLocationApi.getLastLocation(mApiClient);

            if (location == null) {
                LocationServices.FusedLocationApi.requestLocationUpdates(mApiClient, mLocationRequest, this);

            } else {
                //If everything went fine lets get latitude and longitude
                currentLatitude = location.getLatitude();
                currentLongitude = location.getLongitude();
                Log.v("currentLatitude",currentLatitude + " WORKS " + currentLongitude + "");
            }

    }

试试这个代码:

private LocationCoord gps = null;
private static final int PERMISSION_REQUEST_CODE = 1;

在 OnCreate() 中:

//GPS Manage
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    boolean gps_enabled = false;
    boolean network_enabled = false;

    try {
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }

    try {
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
    }

    if (!gps_enabled && !network_enabled) {
        // notify user
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setMessage("Allow ImHere to access this device's location?");
        dialog.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(myIntent);
                //get gps
            }
        });
        dialog.setNegativeButton("Deny", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub

            }
        });
        dialog.show();
    }

    gps = new LocationCoord(this);

@Override
protected void onStart() {
    super.onStart();

    // permission android 6.0
    if (!checkPermission()) {
        requestPermission();
    }

}


private boolean checkPermission(){
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
    if (result == PackageManager.PERMISSION_GRANTED) return true;
    else return false;
}

private void requestPermission(){
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
}

您需要对 Manifest.xml 具有此权限:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<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" />

你可以在这里获取LocationCord.java:https://github.com/toomyy94/ImHere-Chatbot/blob/master/app/src/main/java/pt/ua/tomasr/imhere/modules/LocationCoord.java

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在 android api 23 上打开位置? 的相关文章

  • 如何设置日期选择器的日期?

    I use DatePickerDialog OnDateSetListener效果很好 我想在日期选择器中添加 120 天的日期 我的意思是 如果我添加 120 天 日期和月份将自动更改 怎么做 像这样的事情应该可以解决问题 Calend
  • 避免 XferMode 替换画布上的颜色

    我正在尝试替换使用在画布上绘制的内容的颜色避免Xfer模式 http developer android com reference android graphics AvoidXfermode html 从 android 文档来看 它看
  • 如何解决 MVC 中的“访问令牌已过期,但我们无法刷新它”

    我目前正在研究谷歌API其目的是获取登录者的圈子 我已经有了访问令牌但问题是每当我尝试运行我的代码时它都会返回此异常 访问令牌已过期 但我们无法刷新它 我该如何解决这个问题 var claimsforUser await UserManag
  • 更新语言时操作栏未更新

    我目前正在构建一个应用程序 允许人们在应用程序中更新语言 但是 当更改区域设置时 所有字符串都会更新 但操作栏中的字符串不会更新 对应于清单中的活动名称 仅当应用程序从头开始重新启动时才会考虑语言更改 在不强制应用程序重新启动的情况下更新所
  • 在 Android 中使用 PhoneGap 打开 PDF

    我需要打开一个位于 url 中的 PDF 文件 我需要用 PDF 查看器打开它 有可能的 谢谢大家 此致 我建议使用儿童浏览器插件 https build phonegap com blog childbrowser plugin并使用 G
  • 将倒计时器从 10 秒改为 1 秒

    我有一个倒计时器 它以 1 秒的增量从 10000 毫秒倒计时到 0 毫秒 以使按钮在 10 秒后可单击 尽管计时器是准确的并且按照代码的说明执行操作 但我想更改秒的表示方式 但我不知道如何更改 java void startTimer c
  • Android 线程和处理程序不工作

    我最近重构了一个旧项目 发现无论我做什么 特定部分都不再需要正常运行 本质上 我有一个带有 TextView 的 Activity 该视图按时间间隔从同一类中调用的线程更新 暂停是通过 Thread sleep 完成的 并且使用 Handl
  • 使用 android-async-http (loopj) 发布 JSON/XML

    我在用android async http http loopj com android async http 并且真的很喜欢它 我在发布数据时遇到了问题 我必须按照以下格式将数据发布到 API
  • Fabric Beta 和 APK 拆分

    我根据 ABI 而不是密度来拆分我的应用程序 如下所示 splits abi enable true reset include x86 armeabi armeabi v7a mips arm64 v8a universalApk tru
  • 语音识别和文本转语音

    我想开发一个实现语音识别的应用程序 然后使用文本到语音引擎实现文本到语音 我发布了下面的代码 我使用两个按钮和一个列表视图 一个按钮用于语音识别 另一个按钮用于文本转语音 列表视图用于两者 首先在列表视图中发布语音识别的结果 然后应用程序将
  • 当我滚动 gridview 时图像发生变化

    GridView 从 URL 加载图像 当我滚动 gridview 时 更多图像机会 如何修复它 我尝试过 imageAdapter notifyDataSetChanged gridView invalidateViews 我还没有找到这
  • 具有线程安全性的 eventbus 最佳实践

    我的应用程序具有用户交互活动和后台服务 这是修改数据模型的唯一地方 后台服务监听用户所做的操作以及来自网络的传入消息 因此 可能会出现并发问题 我尝试使用处理程序来防止这些问题 对于事件层 我使用 greenrobots Eventbus
  • 如何在phonegap中同时使用相机API选择多个图像?

    如何同时选择或拾取多个图像phonegap camera API使用时Camera DestinationType FILE URI 我一次只能选择一张图像 我可以使用以下命令在 SD 卡中选择多个文件 包括 txt pdf this ht
  • Google Play 将我的应用程序版本显示为“随设备而变化”

    我最近在 Google Play 商店更新了我的应用程序 在我的 Play 商店应用程序页面中 版本显示为因设备而异我没有上传多个 apk 而是使用单个 apk 更新了我的应用程序 那么为什么它显示版本为因设备而异 是否有任何设置可以删除此
  • 无法将图像从 url 存储到 SD 卡

    我想将图像存储在 mnt sdcard 中 package com Downld file frm net import java io BufferedInputStream import java io File import java
  • 我可以举一个使用 runOnUiThread 显示 toast 的示例吗?

    我搜索了很多地方 但找不到 runOnUiThread 实现的完整工作示例 我尝试了很多 但出现了很多错误 我只想显示线程中的吐司 这是最终的完整代码 感谢所有回复的人 import android app Activity import
  • Android 位图到 WebRtc I420 帧损坏

    尝试通过 WebRtc 流式传输位图 我的 Capturer 类大约如下所示 public class BitmapCapturer implements VideoCapturer VideoSink private Capturer c
  • onActivityResult() Intent 数据始终为 null

    有人可以告诉我为什么Intent data总是为空 Override protected void onActivityResult int requestCode int resultCode Intent data super onAc
  • INSTALL_FAILED_NO_MATCHING_ABIS:无法提取本机库,res = -113设备

    当我在 android 8 0 设备中执行 android 项目时 我收到错误 INSTALL FAILED NO MATCHING ABIS 无法提取本机库 res 113 错误图像 https i stack imgur com 3kb
  • Android ACTION_MOVE阈值

    我正在编写一个应用程序 需要使用手指或最终使用手写笔在屏幕上书写 我有那部分工作 在 ACTION DOWN 时 开始绘制 在 ACTION MOVE 上 添加线段 在 ACTION UP 上 完成该行 问题是 在 ACTION DOWN

随机推荐