Android 新的 LocationSettings 和地理围栏

Posted

技术标签:

【中文标题】Android 新的 LocationSettings 和地理围栏【英文标题】:Android new LocationSettings and Geofencing 【发布时间】:2015-07-08 00:28:38 【问题描述】:

自 7 天以来我一直在尝试这样做,但没有任何结果。问题是 ResultCallback,对于地理围栏我需要 ResultCallback,而对于 LocationSettings 我需要 ResultCallback,有没有办法做到这一点? 我很欣赏一些提示... 我正在使用最新的 Google Play Services 7.0 感谢您的支持:)

public class GeofenceIntentService 
       extends IntentService 
       implements GoogleApiClient.ConnectionCallbacks,
                  GoogleApiClient.OnConnectionFailedListener, 
                  ResultCallback<Status>, ResultCallback<LocationSettingsResult> 

    // onResult for geofencing
    public void onResult(Status status) 
        Log.i(TAG, "onResult");
        if (status.isSuccess()) 
            Log.i(TAG, "status.isSuccess()");
         else 
          String errorMessage = GeofenceErrorMessages.getErrorString(getApplicationContext(),
                status.getStatusCode());
          Log.e(TAG, errorMessage);
        
    

    // onResult for LocationSettings
    @Override
    public void onResult(LocationSettingsResult locationSettingsResult) 
        final Status status = locationSettingsResult.getStatus();
        switch (status.getStatusCode()) 
            case LocationSettingsStatusCodes.SUCCESS:
                Log.i(TAG, "All location settings are satisfied.");
                startLocationUpdates();
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
                        "upgrade location settings ");

                try 
                    // Show the dialog by calling startResolutionForResult(), and check the result
                    // in onActivityResult().
                    status.startResolutionForResult(BaseActivity.this, REQUEST_CHECK_SETTINGS);
                 catch (IntentSender.SendIntentException e) 
                    Log.i(TAG, "PendingIntent unable to execute request.");
                
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                        "not created.");
                break;
        
    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        switch (requestCode) 
            // Check for the integer request code originally supplied to startResolutionForResult().
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode) 
                    case Activity.RESULT_OK:
                        Log.i(TAG, "User agreed to make required location settings changes.");
                        startLocationUpdates();
                        break;
                    case Activity.RESULT_CANCELED:
                        Log.i(TAG, "User chose not to make required location settings changes.");
                        break;
                
                break;
        
    

【问题讨论】:

你能发布一些代码,解释你到目前为止所做的事情,并解释什么是行不通的吗? 是的,看完问题后我不知道你在问什么...... @AlexBaker:下午我会提供一个样本…… @MarianPaździoch:我想在一个活动中使用 LocationSettings + 地理围栏... 【参考方案1】:

我遇到了同样的问题,这就是我所做的

类定义:

public class Activity/Service extends AppCompatActivity/IntentService implements
        ConnectionCallbacks, 
        OnConnectionFailedListener,
        ResultCallback<Status> 
   ...

对于 GeofencingApi:

public void addGeofencesButtonHandler(View view) 
    if (!mGoogleApiClient.isConnected()) 
        // Log.e(TAG, "Not connected");
        return;
    

    try 
        PendingResult<Status> result =
                LocationServices.GeofencingApi.addGeofences(
                        mGoogleApiClient,
                        getGeofencingRequest(),
                        getGeofencePendingIntent()
                );

        result.setResultCallback(this); 
     catch (SecurityException securityException) 
        logSecurityException(securityException);
    

对于 SettingsApi:

使用链式设置结果回调

protected void checkLocationSettings() 
            LocationServices.SettingsApi.checkLocationSettings(
                    mGoogleApiClient,
                    mLocationSettingsRequest
            ).setResultCallback(new ResultCallback<LocationSettingsResult>() 
                @Override
                public void onResult(LocationSettingsResult locationSettingsResult) 
                    final Status status = locationSettingsResult.getStatus();
                    switch (status.getStatusCode()) 
                        case LocationSettingsStatusCodes.SUCCESS:
                            Log.i(TAG, "All location settings are satisfied.");
                            if (!mRequestingLocationUpdates) 
                                mRequestingLocationUpdates = true;
                                setButtonsEnabledState();
                                startLocationUpdates();
                            
                            break;
                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
                                    "upgrade location settings ");
                            try 
                                status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                             catch (IntentSender.SendIntentException e) 
                                Log.i(TAG, "PendingIntent unable to execute request.");
                            
                            break;
                        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                            Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                                    "not created.");
                            break;
                    
                
            );

【讨论】:

以上是关于Android 新的 LocationSettings 和地理围栏的主要内容,如果未能解决你的问题,请参考以下文章

向 Android 添加新的网络承载

Android4.0 添加一个新的Android 键值

Android 6.0 系统棉花糖新的特性和功能

怎么为Android系统添加一个新的资源包

Android新的底部导航栏或BottomNavigationView

android中怎么添加新的功能模块