W.venus W.venus - 1 year ago 97
Android Question

Android - How to fix permission Exception

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() {

public IntergratedLocationManager er;

public Handler _handlerForStartSearch = new Handler(){
public void handleMessage(final Message msg){
case 0:
_handlerForStartSearch.sendEmptyMessageDelayed(0, CommonUtill.updateRate);
case IntergratedLocationManager.ILM_LOCATION_MESSAGE:
final LocationObj loc = (LocationObj) msg.obj;

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

}catch (Exception e){


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

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download