我已经创建了一个基本的应用程序,它将数据存储到Firebase实时数据库中。但是我不知道该应用程序或Firebase有问题。有时我的应用程序不与实时数据库连接,为了将其与数据库连接,我必须先切换飞行模式然后关闭它,然后它才能成功连接至数据库。
我仅在装有android 9版本的vivo手机中遇到过此问题,其余所有移动设备都在正确运行我的应用。
Sharing my project's Github link.
Sharing my app's screen recording to show you the real issue
日志
D/FA: Connected to remote service
D/FA: Application going to the background
D/WebSocket: ws_5 - timed out on connect
D/WebSocket: ws_5 - closed
D/WebSocket: ws_5 - closing itself
D/Connection: conn_5 - Realtime connection failed
D/Connection: conn_5 - closing realtime connection
D/PersistentConnection: pc 0 - Got on disconnect due to OTHER
D/PersistentConnection: pc 0 - Scheduling connection attempt
D/ConnectionRetryHelper: Scheduling retry in 2264ms
D/PersistentConnection: pc 0 - Trying to fetch auth token
D/PersistentConnection: pc 0 - Successfully fetched token, opening connection
D/Connection: conn_6 - Opening a connection binding to the service failed
binding to the service failed
D/WebSocket: ws_6 - timed out on connect
D/WebSocket: ws_6 - closed
D/WebSocket: ws_6 - closing itself
D/Connection: conn_6 - Realtime connection failed
D/Connection: conn_6 - closing realtime connection
D/PersistentConnection: pc_0 - Got on disconnect due to OTHER
D/PersistentConnection: pc_0 - Scheduling connection attempt
D/ConnectionRetryHelper: Scheduling retry in 4252ms
D/PersistentConnection: pc 0 - Trying to fetch auth token
D/PersistentConnection: pc 0 - Successfully fetched token, opening connection
D/Connection: conn_7 - Opening a connection
D/WebSocket: ws_7 - timed out on connect
D/WebSocket: ws_7 - closed
D/WebSocket: ws_7 - closing itself D/Connection: conn_7 - Realtime connection
failed D/Connection: conn_7 - closing realtime connection nip • ***** *inn.
no- a An+ an A' * Ana tr. ATUPD
JAVA代码-
public class MainActivity extends AppCompatActivity {
private EditText Fname,Lname;
private Button show,update;
private RecyclerView disp_data;
private DatabaseReference mUser;
private mAdapter mAdapter;
private List<Users> mList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fname = (EditText) findViewById(R.id.Fname);
Lname = (EditText) findViewById(R.id.Lname);
show = (Button) findViewById(R.id.show);
update = (Button) findViewById(R.id.update);
disp_data = (RecyclerView) findViewById(R.id.disp_data);
mAdapter = new mAdapter(MainActivity.this,mList);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
disp_data.setLayoutManager(linearLayoutManager);
disp_data.setAdapter(mAdapter);
mUser = FirebaseDatabase.getInstance().getReference().child("Users");
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayData();
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateData();
}
});
}
private void updateData(){
String pushId = FirebaseDatabase.getInstance().getReference().child("Users").push().getKey();
final Map<String, Object> messageMap = new HashMap<String, Object>();
messageMap.put("Fname",Fname.getText().toString());
messageMap.put("Lname", Lname.getText().toString());
assert pushId != null;
mUser.child(pushId).setValue(messageMap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "Data updated sucessfully", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Error - "+ e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void displayData(){
mUser.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Users users = snapshot.getValue(Users.class);
mList.add(users);
mAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}}