android中需要google api客户端参数

Posted

技术标签:

【中文标题】android中需要google api客户端参数【英文标题】:Google api client parameter is required in android 【发布时间】:2015-05-26 03:50:49 【问题描述】:

以下代码用于从另一个片段获取当前位置。但是由于Google“android中需要Google api客户端参数”而在日志中出现错误。这使我的应用程序崩溃,所以有人知道确切的问题是什么以及如何解决它...

public class GPSTracker extends Activity implements LocationListener, ConnectionCallbacks, OnConnectionFailedListener
     

private final Context mContext;
protected LocationManager locationManager;  
boolean canGetLocation = false;

private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 20; // 1 minute

Location location; // location
double latitude; // latitude
double longitude; // longitude

SharedData gpsSharedData;
public GoogleApiClient mGoogleApiClient;
private Location mLastLocation;

public GPSTracker(Context context) 
    this.mContext = context;
    getLocation();


/**
 * Method to verify google play services on the device
 * */
public boolean checkPlayServices() 
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(mContext);
    if (resultCode != ConnectionResult.SUCCESS) 
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) 
            GooglePlayServicesUtil.getErrorDialog(resultCode,
                    GPSTracker.this, PLAY_SERVICES_RESOLUTION_REQUEST)
                    .show();
         else 
            Toast.makeText(getApplicationContext(),
                    "This device is not supported.", Toast.LENGTH_LONG)
                    .show();
            finish();
        
        return false;
    
    return true;


// Creating google api client object
public synchronized void buildGoogleApiClient() 
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API).build();


public void getLocation() 
    // TODO Auto-generated method stub
    mLastLocation = LocationServices.FusedLocationApi
            .getLastLocation(mGoogleApiClient);

    if (mLastLocation != null) 
        latitude = mLastLocation.getLatitude();
        longitude = mLastLocation.getLongitude();

     else 
        System.out
                .println("(Couldn't get the location. Make sure location is enabled on the device)");
    


public void stopUsingGPS() 
    if (locationManager != null) 
        locationManager.removeUpdates(GPSTracker.this);
    


/**
 * Function to get latitude
 * */
public double getLatitude() 
    if (location != null) 
        latitude = location.getLatitude();
    

    // return latitude
    return latitude;


public double getLongitude() 
    if (location != null) 
        longitude = location.getLongitude();
    

    // return longitude
    return longitude;


public boolean canGetLocation() 
    return this.canGetLocation;


@Override
public void onLocationChanged(Location location) 
    // TODO Auto-generated method stub




@Override
public void onStatusChanged(String provider, int status, Bundle extras) 
    // TODO Auto-generated method stub



@Override
public void onProviderEnabled(String provider) 
    // TODO Auto-generated method stub



@Override
public void onProviderDisabled(String provider) 
    // TODO Auto-generated method stub



@Override
public void onConnected(Bundle arg0) 
    // TODO Auto-generated method stub
    // Once connected with google api, get the location
    getLocation();




@Override
public void onConnectionSuspended(int arg0) 
    // TODO Auto-generated method stub
    mGoogleApiClient.connect();


@Override
public void onConnectionFailed(ConnectionResult arg0) 

    System.out
            .println("Connection failed: ConnectionResult.getErrorCode() = "
                    + arg0.getErrorCode());

【问题讨论】:

检查你的进口 我正在处理的项目中遇到同样的错误,请提供有关您如何解决问题的最新信息或接受答案。 【参考方案1】:

请在getLocation()的第一行调用buildGoogleApiClient();,你还没有初始化mGoogleApiClient变量。

【讨论】:

以上是关于android中需要google api客户端参数的主要内容,如果未能解决你的问题,请参考以下文章

无法连接到 Google API 客户端

Android 上的 Google Ads API 客户端引发 ProviderNotFoundException

Google API - Android BigQuery 客户端的应用级授权

Google ADs(ADmobs) 需要 API 级别 4.0 Android

用于 android 的 Google MAPs API 限制 2500 个请求/天是每个客户端设备还是每个应用程序密钥?

Google API PHP 客户端授权