W.venus W.venus - 2 months ago 6
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() {
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.

Answer

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.

Comments