未从 Android 地理围栏事件收到 Intent
Posted
技术标签:
【中文标题】未从 Android 地理围栏事件收到 Intent【英文标题】:Intent not received from Android Geofence event 【发布时间】:2016-12-18 12:30:05 【问题描述】:一切都正确构建并在模拟器中运行,但我似乎无法让我的 IntentService 记录任何内容。我敢肯定,我缺少或忽略了一些基本的东西,但我对 android/Java 还是很陌生,目前已经没有什么想法了。
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
private Geofence geofence;
private PendingIntent mGeofencePendingIntent;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LatLng latLng = new LatLng(39.7492350, -104.9913250);
geofence = new Geofence.Builder()
.setRequestId(GEOFENCE_REQ_ID)
.setCircularRegion(latLng.latitude, latLng.longitude, GEOFENCE_RADIUS_IN_METERS)
.setExpirationDuration(GEOFENCE_EXPIRATION)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
private boolean checkPermission()
Log.i(TAG, "MainActivity.checkPermission()");
return (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
private GeofencingRequest getGeofencingRequest()
Log.i(TAG, "MainActivity.getGeofencingRequest()");
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofence(geofence);
return builder.build();
private PendingIntent getGeofencePendingIntent()
Log.i(TAG, "MainActivity.getGeofencePendingIntent()");
if (null != mGeofencePendingIntent)
return mGeofencePendingIntent;
Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
@Override
public void onConnected(Bundle connectionHint)
Log.i(TAG, "MainActivity.onConnected()");
if (checkPermission())
mGeofencePendingIntent = getGeofencePendingIntent();
LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, getGeofencingRequest(), mGeofencePendingIntent);
else
Log.i(TAG, "Permission not granted");
@Override
public void onConnectionSuspended(int i)
Log.i(TAG, "MainActivity.onConnectionSuspended()");
if (null != mGeofencePendingIntent)
LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, mGeofencePendingIntent);
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
Log.i(TAG, "MainActivity.onConnectionFailed()");
每当我使用模拟器或 android 控制台更新该设备的 lat/lng 时,以下服务都不会收到任何意图:
public class GeofenceTransitionsIntentService extends IntentService
public GeofenceTransitionsIntentService()
super(GeofenceTransitionsIntentService.class.getSimpleName());
@Override
protected void onHandleIntent(Intent intent)
Log.i(TAG, "GeofenceTransitionsIntentService.onHandleIntent()");
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError())
int errorCode = geofencingEvent.getErrorCode();
Log.e(TAG, "Location Services error: " + errorCode);
else
Log.i(TAG, "geofencingEvent was successful");
清单:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".GeofenceTransitionsIntentService" />
</application>
我一直在参考以下资源:Android Documentation 和 Google Sample。
【问题讨论】:
我有几件事您没有澄清:1-您是否在真实设备上对其进行了测试? 2-你在指定的纬度/经度范围内吗? 3-是否安装了 Google Play 服务? 4-您是否尝试过打开谷歌地图然后更新纬度/经度?有时这是让 GPS 在模拟器上工作的唯一方法。 请在真机上运行你的应用,然后告诉。 【参考方案1】:位置服务在模拟器中似乎没有完全发挥作用。插入真实设备后,一切都按预期工作。
【讨论】:
以上是关于未从 Android 地理围栏事件收到 Intent的主要内容,如果未能解决你的问题,请参考以下文章
Android Geofences (Location Services) - 如何触发应用内事件