使用runOnUiThread获取当前位置

高塔姆拉库姆

我是android的学习者。我正在尝试获取当前位置的天气。我的应用程序中有一个“刷新”按钮,用于使用最新的天气详细信息更新用户界面。应用加载后,它将显示0.0、0.0和0.0的天气。当我单击“刷新”按钮时,它显示了我当前位置的天气。应用加载后如何获取当前位置的天气?我已使用Google Play服务获取当前位置。下面是我的代码。

MainActivity.java

public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    public String apiKey;
    public double latitude;
    public double longitude;
    public String forecastUrl;
    public static final String TAG = MainActivity.class.getSimpleName();
    private GoogleApiClient mGoogleApiClient;
    protected Location mLastLocation;

    public MainActivity() {
        apiKey = “XXXXXXXXXX";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buildGoogleApiClient();
        mRefreshImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getForecast();
            }
        });
        getForecast();
    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    protected void onResume() {
        super.onResume();
        mGoogleApiClient.connect();
    }

    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    private void getForecast() {
        if (isNetworkAvailable()) {
            forecastUrl = “http://www.forecastUrlGoesHere.com/" + apiKey + “/“ + latitude + "," + longitude;
            toggleRefresh();

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(forecastUrl).build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            toggleRefresh();
                        }
                    });
                }

                @Override
                public void onResponse(Response response) throws IOException {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            toggleRefresh();
                        }
                    });
                    try {
                        String jsonData = response.body().string();
                        if (response.isSuccessful()) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    updateView(); //updates the screen ui
                                }
                            });
                        } else {
                            alertUserAboutError();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {

                    }
                }
            });
        } else {
            Toast.makeText(this, getString(R.string.network_error_message), Toast.LENGTH_LONG).show();
        }
    }

    private void updateView() {
    //code to update ui
    }

    private boolean isNetworkAvailable() {
    //check network availability
    }

    private void alertUserAboutError() {
        AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.show(getFragmentManager(), "error_dialog");
    }

    private void toggleRefresh() {
    }

    @Override
    public void onConnected(Bundle bundle) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            latitude = mLastLocation.getLatitude();
            longitude = mLastLocation.getLongitude();
            Toast.makeText(this, latitude + " " + longitude, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, getString(R.string.no_location_detected), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(TAG, "Connection suspended");
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (connectionResult.hasResolution()) {
            try {
                connectionResult.startResolutionForResult(this, 9000);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        } else {
            Log.i(TAG, "Connection failed, ERROR: " + connectionResult.getErrorCode());
        }
    }
}
杰伊什·埃拉格迪尔

好了,有几种方法可以得到预期的结果。让我为其中之一写下步骤:

a)使用ProgressDialog并在onCreate中启动它

b)从onCreate移动getForecast()

c)在onConnected方法中,一旦有经度和纬度可用,请检查ProgressDialog是否仍然可见。如果是,请关闭此对话框并调用getForecast

ProgressDialog链接http://developer.android.com/reference/android/app/ProgressDialog.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用python获取当前位置

来自分类Dev

在Android中使用GPS获取当前位置

来自分类Dev

如何使用android studio获取当前位置

来自分类Dev

无法使用FusedLocationApi获取当前位置

来自分类Dev

Android Studio使用googleapiclient获取当前位置

来自分类Dev

无法使用谷歌地图获取当前位置

来自分类Dev

获取当前位置错误?

来自分类Dev

如何使用片段类在mapview上获取当前位置?

来自分类Dev

使用parsec获取解析源中的当前位置

来自分类Dev

我无法使用jQuery获取元素的当前位置

来自分类Dev

如何使用Bing Maps SOAP Services获取当前位置?

来自分类Dev

在Cordova / Phonegap中仅使用GPS获取当前位置

来自分类Dev

如何使用Here map js 3 API获取当前位置?

来自分类Dev

使用osmdroid获取当前位置时出现空指针异常

来自分类Dev

使用IOS中的CLLocation获取用户当前位置

来自分类Dev

使用GPS获取用户当前和确切位置

来自分类Dev

在使用swiftui的小部件中如何获取当前位置?

来自分类Dev

使用Roslyn获取当前位置的关键字列表

来自分类Dev

Ruby on Rails使用http url获取当前位置

来自分类Dev

如何使用Swift在Appdelegate中获取当前位置

来自分类Dev

在棉花糖中获取当前位置0,低于23的API使用融合的位置给出确切的当前位置

来自分类Dev

获取当前悬停元素的位置

来自分类Dev

获取当前的键盘光标位置

来自分类Dev

获取UIScrollView的当前位置

来自分类Dev

无法从LocationListener获取当前位置

来自分类Dev

Android GPS获取当前位置

来自分类Dev

如何获取当前的GPS位置?

来自分类Dev

获取当前悬停元素的位置

来自分类Dev

获取ViewPager的当前位置

Related 相关文章

热门标签

归档