Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 這拉位置的~就一行
- mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
- 只要mLastLocation不是null接下來都沒問題
- 喔還有再跑這行前都會確認mGoogleApiClient
- if (mGoogleApiClient != null) {
- if (mGoogleApiClient.isConnected())
- 看下面初始化的地方
- 初始化
- private void initGoogleApi() {
- status = (LocationManager) (this.getSystemService(Context.LOCATION_SERVICE));
- if (mGoogleApiClient == null) {
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- mGoogleApiClient.connect();
- }
- if(!status.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
- LogHelper.printDebugLog("No detect gps");
- String type = SharedpreferencesEditor.getString(SharedpreferencesEditor.Action.WEATHER_DATA_TYPE.key);
- if(type.equals(""))
- DialogUtils.makeAlertDialog(this, getString(R.string.dialog_title_gps_not_open_gps),
- getString(R.string.dialog_message_gps_not_open_gps) + "臺北市中正區", getString(R.string.dialog_btn_confirm));
- else
- DialogUtils.makeAlertDialog(this, getString(R.string.dialog_title_gps_not_open_postal),
- getString(R.string.dialog_message_gps_not_open_postal), getString(R.string.dialog_btn_confirm));
- }
- if (status.isProviderEnabled(LocationManager.GPS_PROVIDER) && status.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
- //如果GPS或網路定位開啟,呼叫locationServiceInitial()更新位置
- LogHelper.printDebugLog("No detect gpc");
- if (mGoogleApiClient != null) {
- if (mGoogleApiClient.isConnected())
- getMyLocation();
- else
- mGoogleApiClient.connect();
- } else {
- Toast.makeText(HomeActivity.this, "請開啟定位", Toast.LENGTH_LONG).show();
- startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); //開啟設定頁面
- }
- }
- }
- callback:
- @Override
- public void onConnected(@Nullable Bundle bundle) {
- getMyLocation();
- }
Add Comment
Please, Sign In to add comment