1

I'm trying to get my location (longitude and latitude) using Skyhook. I built it successfully.

import android.app.IntentService;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.reader.ebook.commomUtill.CommonUtill;
import com.reader.ebook.wxpos.IntergratedLocationManager;
import com.reader.ebook.wxpos.LocationObj;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class LocationService extends IntentService {

    String TAG = "Location_tag";

    public LocationService() {
        super(LocationService.class.getSimpleName());
    }

    public IntergratedLocationManager er;

    public Handler _handlerForStartSearch =  new Handler(){
        @Override
        public void handleMessage(final Message msg){
            switch(msg.what){
                case 0:
                    er.getPos();
                    _handlerForStartSearch.sendEmptyMessageDelayed(0, CommonUtill.updateRate);
                    break;
                case IntergratedLocationManager.ILM_LOCATION_MESSAGE:
                    final LocationObj loc = (LocationObj) msg.obj;

                    try{
                        float current_latitude = loc._latitude;
                        float current_longitude = loc._longitude;
                        CommonUtill.setCoordination(current_latitude, current_longitude);

                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    break;
            }
        }
    };
...
}

But I don't get my location. Instead I get an Exception:

java.lang.SecurityException: caller does not have permission to access phone state

I tried to fix this for 2 days, but i don't suitable answer.

Please help me.

earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
W.venus
  • 470
  • 2
  • 6
  • 19

3 Answers3

2

According to that Exception, you're missing the READ_PHONE_STATE permission.

Add the following line to your Manifest:

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

Since the protectionLevel of this permission is dangerous, on API level 23+ you may need to request this permission at runtime.

Check this link out on runtime permissions.

earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
2

I think you're testing it on a Marshmallow device if so then you may need to request the permission when activity starts or on some specific button press.

First of all make sure you have this permission added in manifest

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

and for Marshmallow and above integration of Run time permission can be a lot easier with some useful libraries, my favorite one is PermissionUtils.

All you need to do is compile the dependency in app level build.gradle

dependencies {
    compile 'rebus:permission-utils:1.0.3'
}

then in onCreate of your activity

PermissionManager.with(YourActivity.this)
    .permission(PermissionEnum.READ_PHONE_STATE)
    .askagain(true)
    .ask();

and that's it. You can find more information here.

halfer
  • 19,824
  • 17
  • 99
  • 186
Atiq
  • 14,435
  • 6
  • 54
  • 69
1

For accessing location you have to add following permission in your manifest:

<uses-permission-sdk-23 android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Satyavrat
  • 469
  • 1
  • 7
  • 24