0

I am trying to develop a Google Map application.

The demo application works fine on a real device but fails on the emulator.

the logcat output is as follows:

java.lang.RuntimeException: Unable to start activity ComponentInfo{il.co.talkie.mapdemo/il.co.talkie.mapdemo.MapDemoActivity}: java.lang.SecurityException: "passive" location provider requires ACCESS_FINE_LOCATION permission.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.SecurityException: "passive" location provider requires ACCESS_FINE_LOCATION permission.
        at android.os.Parcel.readException(Parcel.java:1599)
        at android.os.Parcel.readException(Parcel.java:1552)
        at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:606)
        at android.location.LocationManager.requestLocationUpdates(LocationManager.java:880)
        at android.location.LocationManager.requestLocationUpdates(LocationManager.java:464)
        at il.co.talkie.mapdemo.MapDemoActivity.getMyLocation(MapDemoActivity.java:170)
        at il.co.talkie.mapdemo.MapDemoActivity.onCreate(MapDemoActivity.java:106)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Line 170 of MapDemoActivity is

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,0, this);

The manifest file has these permissions (otherwise it will not work on a real device) as can be seen here:

Manifest.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="il.co.talkie.mapdemo" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<permission
    android:name="com.javapapers.currentlocationinmap.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapDemoActivity"
        android:label="@string/title_activity_map_demo" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

I would like to use the emulator to get my location - it displays the google map and it finds locations of addresses I provide, but it refuses to get my location - so I can not calculate distances properly.

What is wrong? The emulator I use was created for API 23 and target Google API. Any help will be welcomed.

Zvi
  • 2,354
  • 2
  • 26
  • 37
  • OK, from the answer of @headuck I understood that I need to ask for permission in my app, so that it will be compatible with API 23. However when I try to check for permission I get errors: ContextCompat.checkSelfPermission(MainActivity.this, android.permission.READ_SMS); gives an error cannot resolve "permission", and ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_SMS); gives an error cannot resolve "READ_SMS" – Zvi Sep 19 '15 at 14:57
  • Figured it out: although the examples pointed below do not have it bewteen quotes I saw it somewhere else' so the right call is ContextCompat.checkSelfPermission(MainActivity.this, "Manifest.permission.READ_SMS"); – Zvi Sep 19 '15 at 16:36

2 Answers2

0

If you are using emulator on Android M , you will need to request permission from user despite your manifest. See

ACCESS_FINE_LOCATION permission error emulator only

Android M Google API's to use Google Maps and other permissions

You may check out articles / tutorials e.g. this to work with the new system or just target a lower API <= 22

Community
  • 1
  • 1
headuck
  • 2,763
  • 16
  • 19
0

Error is occuring because of this problem:

Caused by: java.lang.SecurityException: "passive" location provider requires ACCESS_FINE_LOCATION permission.

Just add a new permission to your app manifest:

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

Lucky Rana
  • 1,030
  • 1
  • 12
  • 20
  • As noted in the question above the permission is already in the manifest, otherwise it would not work in a real device (likely to be < Android 6). – headuck Sep 18 '15 at 09:46
  • try to use Genymotion Simulator from here https://www.genymotion.com/ , you will not get this problem. in android simultor sometimes location control do not work properly. – Lucky Rana Sep 18 '15 at 09:48