我该如何运行后台活动?

时间:2018-02-09 20:37:53

标签: android service background background-service

我有一个简单的聊天应用程序,我希望在背板上运行ChatActivity,并在从数据库中检索数据时收到通知。但我不明白背景会如何运作。

在最终的CustomAdapter适配器= new CustomAdapter(this,mesajList,firebaseUser); .....部分,消息被接收并显示在屏幕上。我希望这在后台工作

public class SohbetActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
private static String PREFS = "PREFS";
private static int SPLASH_TIME_OUT = 400;
TextView tvBaslik;
EditText et_mesaj;
FloatingActionButton buttonGonder;
ListView lv_chatyap;
FirebaseDatabase database;
FloatingActionButton imageButton;

private AdView mAdView2;

DatabaseReference dbRef;

FirebaseStorage firebaseStorage;
StorageReference storageReference;

public static final int RC_SELECT_IMAGE=1;
final FirebaseUser firebaseUser= FirebaseAuth.getInstance().getCurrentUser();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sohbet);
tvBaslik= (TextView) findViewById(R.id.textViewBaslik);
et_mesaj= (EditText) findViewById(R.id.editTextMesaj);
    buttonGonder= (FloatingActionButton) findViewById(R.id.buttonMesajGonder);
    lv_chatyap= (ListView) findViewById(R.id.listViewChatYap);
    lv_chatyap.setDivider(null);
lv_chatyap.setDivider(null);
    et_mesaj.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(s.toString().trim().length()>0){
                buttonGonder.setEnabled(true);}
            else{
                buttonGonder.setEnabled(false);}
        }

        @Override
        public void afterTextChanged(Editable s) {}
    });

    String oda= getIntent().getStringExtra("odaKey");
    tvBaslik.setText(oda);

    final ArrayList<Mesaj> mesajList=new ArrayList<Mesaj>();

    database=FirebaseDatabase.getInstance();
    dbRef=database.getReference("chats/"+oda);

    firebaseStorage=FirebaseStorage.getInstance();
    storageReference=firebaseStorage.getReference("chatImages/"+oda);

    buttonGonder.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onClick(View v) {
            String gonderen=firebaseUser.getEmail();
            String mesaj=et_mesaj.getText().toString();

            String zaman = new SimpleDateFormat("dd/MM HH:mm").format(Calendar.getInstance().getTime());

            dbRef.push().setValue(new Mesaj(gonderen,mesaj,zaman,null));
            et_mesaj.setText("");

        }
    });

    final CustomAdapter adapter=new CustomAdapter(this,mesajList,firebaseUser);
    dbRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mesajList.clear();
            for(DataSnapshot ds:dataSnapshot.getChildren()){
                mesajList.add(ds.getValue(Mesaj.class));

            }
            lv_chatyap.setAdapter(adapter);
            adapter.notifyDataSetChanged();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

`

1 个答案:

答案 0 :(得分:0)

您需要了解一些事项。

  1. 的jobscheduler
  2. 广播
  3. 广播接收器
  4. 这是流程。

    • 将数据库中的当前对象数存储在Shared Pref中。
    • 创建一个JobScheduler,然后根据共享首页中的当前大小查看数据库是否发生变化。
    • 如果更改,请向您的申请发送广播。
    • BroadcastReceiver将接收该广播,然后向用户发送通知。