Service not available while calling geoCoder.getFr

2019-01-03 09:28发布

I know sometimes google back-end service might not be available.

Hence a solution might be to loop until i get the data.

private class getLocationDetails extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        Log.d("looping", "" + count + "");
        count++;
        double lat = Double.parseDouble(params[0]);
        double lng = Double.parseDouble(params[1]);
        List<Address> addresses = null;
        try {

            Geocoder gCoder = new Geocoder(ImageAndLocationActivity.this,
                    Locale.getDefault());
            addresses = gCoder.getFromLocation(lat, lng, 1);
            Address addr = addresses.get(0);
            user_country = addr.getCountryName();
            user_city = addr.getLocality();
            user_district = addr.getSubAdminArea();

            if (user_city == null) {

                user_city = user_district;
            }
        } catch (Exception e) {

            Log.e("Exception in getLocationDetails - ", e.getMessage());
            return null;
        }

        return "";
    }

    @Override
    protected void onPostExecute(String result) {

        if (result != null) {

            Log.d("user_city = ", "" + user_city);
        } else {

            new getLocationDetails().execute(CurrentLat + "", CurrentLng
                    + "");
        }
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onProgressUpdate(Void... values) {

    }
}

But i am not able to get the location at all:

LogCat:

02-27 16:29:49.568: D/looping(10966): 110355
02-27 16:29:49.568: E/Exception in getLocationDetails -(10966): Service not Available
02-27 16:29:49.573: D/looping(10966): 110356
02-27 16:29:49.573: E/Exception in getLocationDetails -(10966): Service not Available
02-27 16:29:49.573: D/looping(10966): 110357
02-27 16:29:49.573: E/Exception in getLocationDetails -(10966): Service not Available

and ofcourse i have added all the needed permissions:

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

I am trying this on Samsung Galaxy Note GT-N7000 (4.0.4 version)

Am i missing any settings? related to device or application ? Or this usually happens? If so any better solution to resolve this??

Thank You

14条回答
手持菜刀,她持情操
2楼-- · 2019-01-03 10:12

I had the same error, add below permissions to resolve it.

<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 
<uses-permission android:name="android.permission.INTERNET" />
查看更多
Anthone
3楼-- · 2019-01-03 10:14

For the following line

Geocoder gCoder = new Geocoder(context, Locale.getDefault());

Use Context of your Activity and don't use getApplicationContext()

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-03 10:15

The actual reason why Geocoder was not working is because the NetworkLocator was killed in action. Probably due to less memory or maybe you used the Task Manager to kill all services?

I'm not sure but this is a guess. I've seen this before. Last year I wrote a reconnect mechanism to load the NetworkLocator.apk and bind to the GeocoderService. I think this change has not merged into JellyBean so this problem persists.

It can be only solved by reboot. (The NetworkLocationService is loaded at boot)

Edit: You won't see this problem in JBP or KK, this service is moved into the playstore app .

查看更多
看我几分像从前
5楼-- · 2019-01-03 10:19

API will throw a "Service not Available exception" if such service is unavailable on the device. Use method isPresent() to check for the existence of the service.

See also: http://developer.android.com/reference/android/location/Geocoder.html

查看更多
地球回转人心会变
6楼-- · 2019-01-03 10:19

I had the same geocoder error but non of the above applied. It would not run one of my Android devices. Then I remembered that I had accedently killed some running service. The solution was to remove the battery for some seconds and re-install it. And it worked without changing the code :))

查看更多
一夜七次
7楼-- · 2019-01-03 10:20

Some devices do not have suport for Geocoder, so what you need to do is create your own geocoder.

Basicaly you need create a async task to request google for the address and treat the json response.

Using aquery, i do something like this:

public void asyncJson(String address){
        address = address.replace(" ", "+");

        String url = "http://maps.googleapis.com/maps/api/geocode/json?address="+ address +"&sensor=true";

        aq.ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() {

                @Override
                public void callback(String url, JSONObject json, AjaxStatus status) {                        

                        if(json != null){

                                 //here you work with the response json
                                 JSONArray results = json.getJSONArray("results");                               
                                Toast.makeText(context, results.getJSONObject(1).getString("formatted_address"));

                        }else{                                
                                //ajax error, show error code
                                Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
                        }
                }
        });        
}
查看更多
登录 后发表回答