![](/img/trans.png)
[英]Attempting to retrieve location on Android Device... works on emulator but not real device
[英]Firebase won't retrieve data on real device, but works normally on emulator
我正在開發一個帶有 firebase 用於存儲的公共汽車跟蹤應用程序。
我目前檢索電台列表的方法似乎在模擬器中運行良好
該方法需要在 firebase 中獲取我的 Stations 集合中的所有文檔
方法
private void getAllBusStops()
{
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setTimestampsInSnapshotsEnabled(true)
.build();
mDb.setFirestoreSettings(settings);
CollectionReference busStopRef = mDb
.collection(getString(R.string.collection_bus_stops));
busStopRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
ArrayList<BusStop> tempBusStops = new ArrayList<>();
int i = 0;
for (QueryDocumentSnapshot doc : task.getResult())
{
BusStop busStop = doc.toObject(BusStop.class);
GeoPoint getGeoFirebase = (GeoPoint) doc.get("position");
busStop.setPosition(getGeoFirebase);
tempBusStops.add(busStop);
}
dataViewModel.getmBusStopsLiveData().setValue(tempBusStops);
}
});
}
getAllBusStops
的位置@Override
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getAllBusStops();
}
調試顯示可以從firebase取回數據
我嘗試連接我的真實設備進行調試,但是用於獲取 Stations 集合getAllBusStops()
的 firebase function 似乎沒有返回任何快照,我嘗試調試並且來自task.getResult()
的快照數量為 0,在模擬器設備上的數量檢索到的快照數量為 3,與我的 firebase 數據庫中的文檔數量完全相同
總之,這行代碼在調用onComplete事件時可以在模擬器上正常獲取數據
for (QueryDocumentSnapshot doc : task.getResult())
但在真實設備上有 0 個快照
我讀過一些帖子 firebase 需要設置權限,所有權限要求都已在我的項目清單中設置
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
真機已授予所有權限,但仍然沒有數據我真的不明白發生了什么,我該如何解決這個問題?
問題已解決
我追溯了日志,顯然互聯網連接太弱而無法訪問 Firestore 后端
錯誤日志:
W/Firestore: (21.5.0) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
我切換到更強的 wifi 並且應用程序正在運行 好的,這有點奇怪,因為我的 4G 連接的平均速度為 4 MB/S。 當我 go 沿着
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.