K
K
kiddle2017-11-22 18:02:36
Android
kiddle, 2017-11-22 18:02:36

How to give permission in android application?

Hello, I want to get a geo location, but my program is constantly crashing. And it outputs this error:

“gps” location provider requires ACCESS_FINE_LOCATION permission
.
Here's what's in the tips, I tried them, it doesn't work. The program still closes.
5a1590d206ce2608757402.png
The emulator starts version sdk 27. On the phone sdk 21. Also crashes. With what it can be connected?
class TaskActivity : AppCompatActivity() {

    var locationManager: LocationManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_task)
        ...
        doAsyncResult { getTask(token) }

        requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION), 1337)
        locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
    }

    override fun onResume() {
        super.onResume()
        try {
            locationManager?.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 10, 10f, locationListener)
            locationManager?.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 10, 10f, locationListener)
        } catch (e: SecurityException) {
            Log.d("myLogs", e.toString())
        }
    }

    val locationListener: LocationListener = object: LocationListener {
        override fun onLocationChanged(location: Location?) {
            showLocation(location)
        }

        override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onProviderEnabled(p0: String?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onProviderDisabled(p0: String?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

    }

    fun showLocation(location: Location?) {
        if (location == null)
            return
        if (location.provider.equals(LocationManager.GPS_PROVIDER))
            Log.d("myLogs", formatLocation(location) + " GPS")
        else if (location.provider.equals(LocationManager.NETWORK_PROVIDER))
            Log.d("myLogs", formatLocation(location) + " NETWORK")
    }

    fun formatLocation(location: Location?): String {
        if (location == null)
            return ""
        return location.latitude.toString() + " " + location.longitude.toString() + " " + Date(location.time).toString()
    }
    ....
}

Here is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.trackmove.trackmove">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-feature android:name="android.hardware.location.gps" />

    <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>
        <activity android:name=".TaskActivity"></activity>
    </application>

</manifest>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
davidnum95, 2017-11-22
@davidnum95

Here in this line the permissions have not yet been obtained.
You need to call this in the permission request callback.

M
Mikhail Chvarkov, 2017-11-22
@KuSu

The studio tells you otherwise. She says this method does not work on android versions < 6.0.
Regarding your question - requesting and obtaining permissions is not instantaneous. You need before
insert a check to see if permission has been granted. If received, then initialize the LocationManager, if not, then make a permissions request, and only after the user gives permission - try to initialize again.
( you can read more here )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question