【问题标题】:Get the context inside a Java Class获取 Java 类中的上下文
【发布时间】:2015-05-30 14:05:15
【问题描述】:

我创建了以下类

package com.salvo.weather.android.geolocation;

import android.location.Location;
import android.os.Bundle;


import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
import com.salvo.weather.android.entity.CurrentGeolocationEntity;

/**
 * Created by mazzy on 30/05/15.
 */
public class CurrentGeolocation implements ConnectionCallbacks, OnConnectionFailedListener {

    private GoogleApiClient mGoogleApiClient;

    private CurrentGeolocationEntity mCurrentGeolocationEntity;

    public CurrentGeolocation() {
        buildGoogleApiClient();
        this.mCurrentGeolocationEntity = new CurrentGeolocationEntity();
    }

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

    public GoogleApiClient getmGoogleApiClient() {
        return mGoogleApiClient;
    }

    @Override
    public void onConnected(Bundle bundle) {
        this.mCurrentGeolocationEntity.setmLastLocation(LocationServices
                .FusedLocationApi
                .getLastLocation(this.mGoogleApiClient));

        Location mLastLocation = this.mCurrentGeolocationEntity.getmLastLocation();

        if (mLastLocation != null) {
            this.mCurrentGeolocationEntity.setmLongitude(mLastLocation.getLongitude());
            this.mCurrentGeolocationEntity.setmLatitude(mLastLocation.getLatitude());
        } else {
            // TODO: Add toast
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        // The connection to Google Play services was lost for some reason. We call connect() to
        // attempt to re-establish the connection.
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // Refer to the javadoc for ConnectionResult to see what error codes might be returned in
        // onConnectionFailed.
    }
}

我需要检索上下文以便在 GoogleApiClient 中传递它。你说什么?这是个好办法吗?我是Android编程的初学者

【问题讨论】:

标签: java android geolocation android-context


【解决方案1】:

定义一个全局变量:

 Context context = null;

将构造函数更改为:

public CurrentGeolocation(Context context) { 
        buildGoogleApiClient(); 
        this.context = context;
        this.mCurrentGeolocationEntity = new CurrentGeolocationEntity();
    } 

现在正在调用 Activity :

  CurrentGeolocation obj = new CurrentGeolocation(this);

或片段:

 CurrentGeolocation obj = new CurrentGeolocation(getActivity());

【讨论】:

  • 独一无二!强烈建议通过构造函数传递 Activity 的上下文,这是一种很好的做法。如果它适合你,请接受它的答案。
  • 良好做法,只要您的类是从您的 Activity 实例化并在 Activity 被销毁时随之消亡。
猜你喜欢
  • 2019-12-30
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 2014-10-11
  • 2020-04-13
  • 1970-01-01
相关资源
最近更新 更多