【问题标题】:Android : Geo Fencing Receiver not callingAndroid:地理围栏接收器不调用
【发布时间】:2014-02-19 16:17:27
【问题描述】:

我刚刚根据文档在 android 中为地理围栏制作了一个演示,但是当我从围栏区域出去或进入围栏区域时,这里地理围栏广播接收器没有调用。

大家查看代码并尝试找出问题所在。

感谢金哥

SplashActivity.java

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        List<Geofence> mCurrentGeofences = new ArrayList<Geofence>();

        SimpleGeofence mUIGeofence1 = new SimpleGeofence("1",
        // Get latitude, longitude, and radius from the UI
                26.87473379143512, 75.78373555021025, 1,
                // Set the expiration time
                Geofence.NEVER_EXPIRE,
                // Only detect entry transitions
                Geofence.GEOFENCE_TRANSITION_ENTER);

        /*
         * Add Geofence objects to a List. toGeofence() creates a Location
         * Services Geofence object from a flat object
         */
        mCurrentGeofences.add(mUIGeofence1.toGeofence());

        // Start the request. Fail if there's already a request in progress
        try {
            // Try to add geofences
            GeofenceRequester mGeofenceRequester = new GeofenceRequester(this);
            mGeofenceRequester.addGeofences(mCurrentGeofences);
        } catch (UnsupportedOperationException e) {
        }

    }
}

GeoFenceRequestor.java

private PendingIntent createRequestPendingIntent() {

    // If the PendingIntent already exists
    if (null != mGeofencePendingIntent) {

        // Return the existing intent
        return mGeofencePendingIntent;

        // If no PendingIntent exists
    } else {

        // Create an Intent pointing to the IntentService
        Intent intent = new Intent(
                "com.example.demo.ACTION_RECEIVE_GEOFENCE");

        // MAKE SURE YOU CHANGE THIS TO getBroadcast if you are coming from
        // the sample code.
        return PendingIntent.getBroadcast(mActivity, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }
}


@Override
public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) {

    // Create a broadcast Intent that notifies other components of success
    // or failure
    Intent broadcastIntent = new Intent();

    // Temp storage for messages
    String msg;

    // If adding the geocodes was successful
    if (LocationStatusCodes.SUCCESS == statusCode) {

        // Create a message containing all the geofence IDs added.
        msg = mActivity.getString(R.string.add_geofences_result_success,
                Arrays.toString(geofenceRequestIds));

        // In debug mode, log the result
        Log.d(GeofenceUtils.APPTAG, msg);

        // Create an Intent to broadcast to the app
        broadcastIntent.setAction(GeofenceUtils.ACTION_GEOFENCES_ADDED)
                .addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES)
                .putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, msg);
        // If adding the geofences failed
    } else {

        /*
         * Create a message containing the error code and the list of
         * geofence IDs you tried to add
         */
        msg = mActivity.getString(R.string.add_geofences_result_failure,
                statusCode, Arrays.toString(geofenceRequestIds));

        // Log an error
        Log.e(GeofenceUtils.APPTAG, msg);

        // Create an Intent to broadcast to the app
        broadcastIntent.setAction(GeofenceUtils.ACTION_GEOFENCE_ERROR)
                .addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES)
                .putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, msg);
    }

    // Broadcast whichever result occurred
    LocalBroadcastManager.getInstance(mActivity).sendBroadcast(
            broadcastIntent);

    // Disconnect the location client
    requestDisconnection();
}

GeofenceReceiver.java

公共类 GeofenceReceiver 扩展 BroadcastReceiver { 上下文上下文;

    Intent broadcastIntent = new Intent();

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;

        broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);
        Toast.makeText(context, "Receiver", Toast.LENGTH_LONG).show();
        if (LocationClient.hasError(intent)) {
            handleError(intent);
        } else {
            handleEnterExit(intent);
        }
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />

        <activity
            android:name=".SplashActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".GeofenceReceiver"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.example.demo.ACTION_RECEIVE_GEOFENCE" />
            </intent-filter>
        </receiver>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

成功添加地理围栏,但后来它不调用广播接收器来了解是否进入/退出

【问题讨论】:

  • 在您的SplashActivity 中,您没有处理异常(通常是坏主意)。您的变量 mGeofenceRequester 几乎立即超出范围...您确定要发生这种情况吗?
  • 我上面的代码中没有出现异常,只是接收者没有调用,但它应该
  • 你解决了这个问题吗?

标签: android android-geofence


【解决方案1】:

Android GeoFences 从不启用 GPS(因为它们的 API 很糟糕,而且它们的设备功耗已经失控)。如果您想通过 GPS 进行地理围栏,您必须设置您的地理围栏,然后不断地单独轮询 GPS。

【讨论】:

  • 真的吗?轮询 GPS?你有那个链接吗?
  • 根据我在 2014 年的评论,这就是我使用的 Android 版本 (KitKat iirc) 的现实。我也不敢相信。关于它如何决定使用 GPS 的位置 API 的官方文档几乎不存在。您可以简单地将 GPS 包含在位置 API 使用的输入中,仅此而已。我通过实验发现,“手动”激活 GPS 硬件会迫使新的 GPS 坐标进入该 API,从而使我的地理围栏以我希望的精度工作。也许定位子系统已经改进了我多年没有做过 Android 开发。
猜你喜欢
  • 1970-01-01
  • 2014-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 2016-12-18
相关资源
最近更新 更多