Firebase Android版模拟器正常运行,三星S5真机无addValueEventListener回调求助
Hey there! It’s super frustrating when your code works flawlessly on an emulator but breaks on a real device—let’s walk through the most common fixes for this Firebase callback issue you’re facing.
1. Check Firebase SDK & Google Play Services Compatibility
Your Samsung S5 is running Google Play Services 12.5.29, which is pretty outdated. Newer Firebase SDK versions require a minimum Play Services version to function properly.
- If you’re using the Firebase Bill of Materials (BOM) in your
build.gradle, try downgrading to an older BOM version that’s compatible with Play Services 12.x. For example, BOM version16.0.0is known to work well with older Play Services releases. - Alternatively, look up Firebase release notes to find SDK versions that explicitly support Play Services 12.5.29, then specify those individual SDK versions instead of using the BOM.
2. Verify Network Connectivity & Permissions
Real devices can have network quirks emulators don’t:
- Make sure your Samsung S5 has a stable internet connection (try switching between WiFi and mobile data to rule out network-specific issues).
- Double-check that your app has the
INTERNETpermission inAndroidManifest.xml:<uses-permission android:name="android.permission.INTERNET" /> - Some networks (like corporate WiFi) might block Firebase’s endpoints—test on a different network to eliminate this possibility.
3. Check Firebase Database Security Rules
If your database rules are too restrictive, your device might be blocked from reading data, which would prevent onDataChange from firing.
- Temporarily set your rules to open (for testing only!) to see if that fixes the issue:
Important: Revert to secure rules once you confirm this is the root cause.{ "rules": { ".read": true, ".write": true } }
4. Force Sync or Disable Persistence
Firebase’s offline persistence might be causing unexpected behavior on older devices:
- Try enabling
keepSyncedon your reference to force it to sync with the server:ref!!.keepSynced(true) - If you’ve enabled persistence elsewhere in your code, try disabling it temporarily to test:
FirebaseDatabase.getInstance().setPersistenceEnabled(false)
5. Debug with Detailed Logging
Your current onCancelled method only logs a generic message—expand it to capture actual error details that can point you to the problem:
override fun onCancelled(p0: DatabaseError?) { Log.d("firebase", "Cancelled: ${p0?.message} | Error Details: ${p0?.details}") }
This will tell you if there’s an authentication failure, permission denial, or network issue stopping the callback from firing.
Your Original Code for Reference
ref = FirebaseDatabase.getInstance().reference ref!!.addValueEventListener(object : ValueEventListener { override fun onCancelled(p0: DatabaseError?) { Log.d("firebase", "cancelled") } override fun onDataChange(p0: DataSnapshot?) { // 未触发该回调 } })
Start with checking the Play Services/SDK compatibility first—this is the most common culprit for issues on older devices like the S5. Let me know if any of these steps get your callback working!
内容的提问来源于stack exchange,提问作者Paragon

