Android Geofence的学习(三)总结、Demo和问题

转载请注明:http://blog.csdn.net/btyh17mxy/article/details/9038443 

官方给出了一个demo:http://developer.android.com/shareables/training/GeofenceDetection.zip

Geofence是一个基于Google Play Services的虚拟地理区域,是一个由中心点经纬度和半径描述的圆形区域。Location Service会以低功耗的方式获取用户的位置,当用户进入或退出Geofence范围时会通知应用,应用接受到通知后可采取相应的操作,例如在通知栏显示这样的通知:


一、方法

1、检查Google Play Services是否可用



	/**
	 * 驗證Google Play Services是否可用
	 * 
	 * @return 可用返回真否則返回假
	 */
	private boolean servicesConnected() {
		// 檢查服務是否可用
		int resultCode = GooglePlayServicesUtil
				.isGooglePlayServicesAvailable(this);
		// 如果可用
		if (ConnectionResult.SUCCESS == resultCode) {
			// Log狀態
			Log.d(GeofenceUtils.APPTAG,
					getString(R.string.play_services_available));
			return true;
		// 如果不可用
		} else {
			// 顯示錯誤提示對話框
			Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode,
					this, 0);
			if (dialog != null) {
				ErrorDialogFragment errorFragment = new ErrorDialogFragment();
				errorFragment.setDialog(dialog);
				errorFragment.show(getSupportFragmentManager(),
						GeofenceUtils.APPTAG);
			}
			return false;
		}
	}

在应用启动的时候就应该检查一下GoogleServices是否可用,如果不可用的话是不能使用Geofence的。

PS:可能是输入法抽风,在Eclipse里面总是打出繁体字来

2、连接和断开LocationClient、添加和移除Geofence(s)、处理连接异常

LocationClient的原型是:

com.google.android.gms.location.LocationClient.LocationClient(Context context, ConnectionCallbacks connectionCallbacks, OnConnectionFailedListener connectionFailedListener)
在官方的例子中是写了一个持有主调Activity强引用并导入上述几个回调接口的类来处理添加Geofence请求。像这样:

/**
 * 连接Google Play Services和请求Geofence
 * 
 * <b>
 * Note: 必须首先验证Google Play Services可用
 * </b> Use GooglePlayServicesUtil.isGooglePlayServicesAvailable() to check.
 *
 * 调用 AddGeofence()方法添加Geofence
 *
 */

public class GeofenceRequester
                implements
                    OnAddGeofencesResultListener,
                    ConnectionCallbacks,
                    OnConnectionFailedListener {

    // 主调Activity的强引用
    private final Activity mActivity;
    
    // Stores the PendingIntent used to send geofence transitions back to the app
    private PendingIntent mGeofencePendingIntent;

    // Stores the current list of geofences
    private ArrayList<Geofence> mCurrentGeofences;

    // Stores the current instantiation of the location client
    private LocationClient mLocationClient;

    /*
     * Flag that indicates whether an add or remove request is underway. Check this
     * flag before attempting to start a new request.
     */
    private boolean mInProgress;

    public GeofenceRequester(Activity activityContext) {
        // Save the context
        mActivity = activityContext;

        // Initialize the globals to null
        mGeofencePendingIntent = null;
        mLocationClient = null;
        mInProgress = false;
    }

    /**
     * Set the "in progress" flag from a caller. This allows callers to re-set a
     * request that failed but was later fixed.
     *
     * @param flag Turn the in progress flag on or off.
     */
    public void setInProgressFlag(boolean flag) {
        // Set the "In Progress" flag.
        mInProgress = flag;
    }

    /**
     * Get the current in progress status.
     *
     *
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 18
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值