我試圖構建一個地理圍欄應用程序接收意圖,但似乎在主要活動開始僅註冊地理圍欄,並意圖服務停止接收他們,當應用程序被關閉。因此,我將添加geofence邏輯移入intent服務(以及意圖處理代碼)並確保服務開始,但現在該服務根本沒有收到任何意圖!Android應用程序添加地理圍欄和相同的服務
服務定義
public class GeofenceTransitionsIntentService extends IntentService implements ConnectionCallbacks, OnConnectionFailedListener, ResultCallback<Status>
一切在服務(谷歌API客戶端生成並連接)在onCreate
完成的,同時與意圖處理程序和地理圍欄登記東西onConnected
寄存器地理圍欄等,基本上,我我試圖在同一個服務中實施大量借用的geofencing示例代碼(來自文檔),目的是處理這些意圖。
所有主要的活動確實是啓動該服務,並提請相關的服務接收地理圍欄通知的事情。
如果您需要更多的信息,只是讓我知道。
編輯
好了,所以它看起來像我們需要更多的信息 - 該服務的概述:
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"
來幫助所有的服務定義。
我不是一個Android開發者* – opticaliqlusion
也許這個問題可以幫助你。 http://stackoverflow.com/questions/21090674/android-geofencing-no-coming-intents?rq=1 – TychoTheTaco
@TychoTheTaco感謝您的迴應,但它似乎沒有任何影響 - 添加「導出」,重建和重新啓動應用程序+服務,仍然沒有。更令人憤怒的是,沒有調試輸出來說明爲什麼它可能不工作! – opticaliqlusion