[英]Geofence transition PendingIntent blocked by the OS on Android Oreo
[英]Android Geofence Transition PendingIntent seems not to run (react-native bridge)
我正在按照android guide android guide來構建一個簡單的本機橋,用於geofencing的react-native。
但是進入或離開地理圍欄時我沒有任何反應。 似乎PendingIntent / IntentService for Transitions無法正常運行。
MyModule基本上看起來像這樣。 它還像在文檔中創建mGeofenceList
一樣,其中填充了react-native的數據。
MyModule的:
public class MyModule extends ReactContextBaseJavaModule {
//Build geofences
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(mGeofenceList);
return builder.build();
}
//Build pending intent
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(reactContext, GeofenceTransitionsIntentService.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
// calling addGeofences() and removeGeofences().
mGeofencePendingIntent = PendingIntent.getService(reactContext, 0, intent, PendingIntent.
FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
@ReactMethod
public void startMonitoring() {
mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i(TAG, "Start Monitoring");
postNotification("Start Monitoring", "Pressed Start Monitoring");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Start Monitoring: " + e.getMessage());
}
});
}
}
運行startMonitoring()
,將生成通知(“開始監視”)和日志,因此我認為該部分中沒有錯誤。
IntentService看起來也很基本/類似於文檔。 IntentService:
public class GeofenceTransitionsIntentService extends IntentService {
private static final String TAG = "GeofenceService";
private Handler handler;
SharedPreferences sp;
public GeofenceTransitionsIntentService(){
super(TAG);
}
@Override
public void onCreate() {
super.onCreate();
sp = PreferenceManager.getDefaultSharedPreferences(this);
handler = new Handler();
Log.i(TAG, "Intent created");
}
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "onHandleIntent");
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = "Error Code: " + String.valueOf(geofencingEvent.getErrorCode());
Log.e(TAG, errorMessage);
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
// Get the geofences that were triggered. A single event can trigger
// multiple geofences.
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
// Get the transition details as a String.
String geofenceTransitionDetails = getGeofenceTransitionDetails(
geofenceTransition,
triggeringGeofences
);
// Send notification and log the transition details.
//sendNotification(geofenceTransitionDetails);
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Enter/Exit", Toast.LENGTH_SHORT).show();
}
});
Log.i(TAG, geofenceTransitionDetails);
} else {
// Log the error.
Log.e(TAG, "Invalid transition");
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();
}
});
}
}
/*
Helpfunctions for logging
*/
private String getGeofenceTransitionDetails(
int geofenceTransition,
List<Geofence> triggeringGeofences) {
String geofenceTransitionString = getTransitionString(geofenceTransition);
// Get the Ids of each geofence that was triggered.
ArrayList<String> triggeringGeofencesIdsList = new ArrayList<>();
for (Geofence geofence : triggeringGeofences) {
triggeringGeofencesIdsList.add(geofence.getRequestId());
}
String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);
return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
}
private String getTransitionString(int transitionType) {
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
return "entered Geofence";
case Geofence.GEOFENCE_TRANSITION_EXIT:
return "exit Geofence";
default:
return "unknown Transition";
}
}
}
但是此類的任何輸出都不會產生!
在本機模塊的清單中,我添加了權限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
在使用該模塊的TestApplication的清單中,我也添加了此權限,並在我添加的應用程序標簽中
<service android:name="com.mymodule.GeofenceTransitionsIntentService" android:exported="false"/>
我無法在模塊清單中添加最后一行,因為它缺少應用程序標簽並且沒有活動。 我不確定這是否是正確的地方。
我正在模擬器中進行測試,並將位置更改為GPS數據播放列表。
問題
當然:3.我的錯誤在哪里?
好吧,回答我自己的問題,或僅回答特定問題3:
上面的代碼沒有錯誤,或者至少沒有錯誤,並且可以在硬件設備上正常工作。
那么,如何在模擬器上正確調試Geofencing?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.