【问题标题】:Kotlin - failed to get location address in real deviceKotlin - 无法在真实设备中获取位置地址
【发布时间】:2020-09-05 22:18:26
【问题描述】:

我的小应用有问题。

我想在 android studio 中使用 geocoding-reverse 获取地址位置。

但我的代码只能在模拟器中工作。当我用应用程序连接我的真实设备并想要获取地址时,它给了我错误并且应用程序崩溃了。错误:grpc 失败。

我将代码放入 try and catch 方法中,但错误仍然存​​在。

这是我的小应用程序:

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.my_test_app">
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyCutiOvAqZiLvka-kW9B12K.........." />

    </application>

</manifest>

xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    >


    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="text1"
        android:textSize="20sp"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text2"
        android:layout_marginTop="10dp"
        android:textSize="20sp"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/textview1"
        />


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textview2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="button"
        android:textSize="25sp"
        />



</RelativeLayou

主要活动:

package com.example.my_test_app

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.Address
import android.location.Geocoder
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.loader.content.AsyncTaskLoader
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import com.google.android.gms.tasks.OnSuccessListener
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity() {

    lateinit var fusedLocationProviderClient: FusedLocationProviderClient
    private val requestcode = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val button = findViewById<Button>(R.id.button)



        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)

        button.setOnClickListener {

            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.ACCESS_FINE_LOCATION
                ) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.ACCESS_COARSE_LOCATION
                ) == PackageManager.PERMISSION_GRANTED
            ) {

                getlocation()


            } else {

                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION
                    ),
                    requestcode
                )

                return@setOnClickListener

            }


        }

    }

    private fun getlocation() {

        fusedLocationProviderClient.lastLocation.addOnSuccessListener(this) { location ->


            if (location != null) {

                val lat = location.latitude
                val long = location.longitude

                val geocoder = Geocoder(this, Locale.getDefault())

                try {

                    val address: List<Address> = geocoder.getFromLocation(lat, long, 1)

                    textview1.text = address[0].maxAddressLineIndex.toString()
                    textview2.text = address[0].getAddressLine(0).toString()


                } catch (e: Exception) {

                    Toast.makeText(this, e.message.toString(), Toast.LENGTH_LONG).show()
                }


            } else {


                Toast.makeText(this, "موقعیت پیدا نشد ", Toast.LENGTH_LONG).show()

            }

        }


    }


}

那么,我的代码有什么问题? Grpc错误是什么意思?

如果我将地理代码放在后台线程中,问题会解决吗?

有人可以帮忙吗?

我使用的是调试 api 密钥而不是释放。

谢谢你的帮助。

【问题讨论】:

    标签: java android google-maps kotlin android-service


    【解决方案1】:

    Geocoder 需要在应用的主线程之外调用。

    Geocoder 是同步的,可能需要很长时间才能完成它的工作,所以您 should not call it from the main user interface (UI) thread 您的应用程序。

    试试AsyncTask..

    还要检查您是否有良好的互联网并且没有使用代理..

    【讨论】:

    • 我搜索并发现 AsyncTask 已弃用。你知道我在后台做代码需要做什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多