【发布时间】:2016-12-23 03:50:27
【问题描述】:
我正在尝试构建一个地理围栏应用程序,但它似乎只在主要活动开始时注册地理围栏,并且当应用程序关闭时意图服务停止接收它们。因此,我将添加地理围栏逻辑(连同意图处理代码)移到了意图服务中,并确保该服务已启动,但现在该服务根本没有收到任何意图!
服务定义
public class GeofenceTransitionsIntentService extends IntentService implements ConnectionCallbacks, OnConnectionFailedListener, ResultCallback<Status>
服务中的所有内容(构建和连接的谷歌 api 客户端)都在 onCreate 中完成,意图处理程序和地理围栏注册东西 onConnected 注册地理围栏等。基本上,我试图实现大量借用用于处理这些意图的同一服务中的地理围栏示例代码(来自文档)。
所有主要活动都是启动服务并绘制与服务中收到的地理围栏通知相关的内容。
如果您需要更多信息,请告诉我。
编辑
好吧,看来我们需要更多信息——服务大纲:
public class GeofenceTransitionsIntentService extends IntentService implements ConnectionCallbacks, OnConnectionFailedListener, ResultCallback<Status> {
protected static final String TAG = "GeofenceTransitionsIS";
protected GoogleApiClient mGoogleApiClient;
protected ArrayList<Geofence> mGeofenceList;
private boolean mGeofencesAdded;
private PendingIntent mGeofencePendingIntent;
private SharedPreferences mSharedPreferences;
public GeofenceTransitionsIntentService() {
super(TAG);
}
@Override
public void onCreate() {
super.onCreate();
buildGoogleApiClient();
populateGeofenceList();
mGoogleApiClient.connect();
}
...
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
// handle the intent, send a notification
}
private void sendNotification(String notificationDetails) {
// sends a notification
}
@Override
public void onConnected(Bundle connectionHint)
{
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
).setResultCallback(this);
}
// straight out of the example
private GeofencingRequest getGeofencingRequest()
{
...
}
// from a branch of the example that reuses the pending intent
private PendingIntent getGeofencePendingIntent()
{
if (mGeofencePendingIntent != null)
{
return mGeofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
mGeofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
public void populateGeofenceList() {
for (thing place : listofplaces) {
mGeofenceList.add(...)
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
public void onResult(Status status)
{
// were fences added? usually yes
}
}
我的研究令人沮丧——我看到有人声称能够通过广播接收器(请参阅第一条评论)而不是通过服务执行此类操作?
从我一直在处理的所有修订中,我有一个非常混乱的 manifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".GeofenceTransitionsIntentService"
android:exported="true"
android:enabled="true">
<intent-filter >
<action android:name="com.aol.android.geofence.ACTION_RECEIVE_GEOFENCE"/>
</intent-filter>
</service>
...
</application>
将intent-filter 和android:exported="true" 添加到服务定义都没有任何帮助。
【问题讨论】:
-
我不是安卓开发者*
-
也许这个问题可以帮助你。 stackoverflow.com/questions/21090674/…
-
@TychoTheTaco 感谢您的回复,但它似乎没有任何效果 - 添加“导出”,重建并重新启动应用程序 + 服务,仍然没有。更令人气愤的是,没有调试输出表明它可能无法正常工作!
-
应用关闭后如何确保服务重启?
-
我关闭了应用程序,然后在设备设置菜单中关闭了服务,然后重新启动应用程序并从服务启动查看所有调试日志
标签: android geofencing android-geofence