我正在尝试使用firebase使用子事件listerner但是它在activty上运行已经开始并将数据添加到我的领域
是否有任何方法只有在数据更改时才能运行?
我在其中运行oncreate
chatchildevntlistener = chatlistreference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Constant.l("Add Changed Listerner Running");
Conversion_List chatconbersionlist = dataSnapshot.getValue(Conversion_List.class);
if (chatconbersionlist.getReceiverid().equals(Session.getUserID(getApplicationContext())) && chatconbersionlist.getSenderid().equals(getIntent().getStringExtra("rid"))) {
chatrealm = Realm.getDefaultInstance();
chatrealm.beginTransaction();
final Chat_history_pojo chatupdatepojo = chatrealm.createObject(Chat_history_pojo.class);
int nextID = chatrealm.where(Chat_history_pojo.class).max("chatnumber").intValue() + 1;
chatupdatepojo.setChatnumber(nextID);
chatupdatepojo.setSenderid(chatconbersionlist.getSenderid());
chatupdatepojo.setReceiverid(chatconbersionlist.getReceiverid());
chatupdatepojo.setTimeofthemessage(chatconbersionlist.getTimeofthemessage());
chatupdatepojo.setType(chatconbersionlist.getType());
chatupdatepojo.setMessage(chatconbersionlist.getMessage().replace("$", " "));
chatupdatepojo.setSender_profile(chatconbersionlist.getSender_profile());
chatupdatepojo.setReciever_profile(chatconbersionlist.getReciever_profile());
chatupdatepojo.setImageprice("0");
chatupdatepojo.setReadstatus(0);
if (!chatconbersionlist.getPoofingtext().equals("0")) {
chatupdatepojo.setPoofingtext(Integer.parseInt(chatconbersionlist.getPoofingtext()));
Intent poofingintent = new Intent(getApplicationContext(), Poofing_Receiver.class);
poofingintent.putExtra("rid", chatconbersionlist.getReceiverid());
poofingintent.putExtra("chatnumberposition", nextID);
PendingIntent poofingpendingintent = PendingIntent.getBroadcast(getApplicationContext(), nextID, poofingintent, 0);
AlarmManager poofingalaramanager = (AlarmManager) getSystemService(ALARM_SERVICE);
poofingalaramanager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + Integer.parseInt(chatconbersionlist.getPoofingtext()) * 1000, poofingpendingintent);
}
chatrealm.commitTransaction();
chatrealm.close();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
// Constant.l(dataSnapshot.toString());
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
Constant.l(databaseError.toString());
}
});
chatlistreference.addChildEventListener(chatchildevntlistener);