I am trying to obtain GPS co-ordinates via a handler. Below is the code :
final Thread ObainGpsBackground = new Thread(new Runnable() {
@Override
public void run() {
try{
String SetServerString = "";
//Obtaining GPS co-ordinates :
LatLong obj_latLong = new LatLong(getActivity());
addressList = obj_latLong.getListAddressFromGeocoder(getActivity());
//Setting the addressList.getLatitude() into a variable "SetServerString"
for (Address address : addressList) {
SetServerString = String.valueOf(address.getLatitude());
threadMsg(SetServerString);
}
//Obtaining GPS co-ordinates :
}
catch(Throwable t){
Log.i("Exception","Getting GPS exception : "+t);
}
}
});
Somehow the code is always navigating towards the catch block. All I get this error :
I/Exception: Getting GPS exception : java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Here is the handler :
private void threadMsg(String setServerString) {
if (!setServerString.equals("") && !setServerString.equals("0")) {
Message msgObj = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", setServerString);
msgObj.setData(b);
handler.sendMessage(msgObj);
}
}
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
// ALERT MESSAGE
// alert_attendance(getResources().getString(R.string.warning), getResources().getString(R.string.ects));
Toast.makeText(getActivity(), "Response: "+aResponse, Toast.LENGTH_SHORT).show();
}
else
{
// ALERT MESSAGE
alert_attendance(getResources().getString(R.string.warning), getResources().getString(R.string.unableToFindLocation));
// Toast.makeText(getBaseContext(), "Not Got Response From Server.", Toast.LENGTH_SHORT).show();
}
}
};
Seen various posts. Somehow not able to figure it out. How to exactly implement looper.prepare() ? Any ideas ?