簡體   English   中英

當活動處於 onPause 生命周期時嘗試運行 aynktask

[英]try to run asynktask when activity is in onPause lifecycle

我需要在我的數據庫發生變化時發出通知,我不能在這個項目中使用 firebase,所以我嘗試在這里提出一個解決方案:(如果我為 check DB 制作了 asynktask 並且它定期重復,那么問題就解決了)但我很迷戀當我嘗試在活動處於 onPause() 情況下運行 aynktask 時,它不起作用!!!

@Override
public void onPause() {
    super.onPause();
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            new push_notification().execute();
        }
    }, 5000);
}

………………………………………………………………………………………………………………………………………………………………………………

private class push_notification extends AsyncTask < Void, Void, Void > {
    HashMap < String,
    String > contact;

    ContactsContract.Contacts contacts;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("please wait ...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void...arg0) {
        ///////////////////////////////
        contactList = new ArrayList < > ();

        HttpHandler sh = new HttpHandler();

        String link;
        String data = "";
        BufferedReader bufferedReader;
        String result;

        try {
            data = "?id=" + URLEncoder.encode("199", "UTF-8");
        } catch (Exception e) {}
        url4 = "http://www.xxxxxxxxxxxxxxxxx.php" + data;
        jsonStr = sh.makeServiceCall(url4);

        if (jsonStr != null) {

            try {
                JSONArray contacts = new JSONArray(jsonStr);
                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);
                    pre_post_id = c.getString("id");
                    pre_phone = c.getString("phone");
                    pre_username = c.getString("username");
                    pre_pic = c.getString("pic");
                    pre_date = c.getString("date");
                    pre_follow_askers = c.getString("follow_askers");

                    contact = new HashMap < > ();
                    // adding each child node to HashMap key => value
                    contact.put("id", pre_post_id);
                    contact.put("phone", pre_phone);
                    contact.put("username", pre_username);
                    contact.put("pic", pre_pic);
                    contact.put("date", pre_date);
                    contact.put("follow_askers", pre_follow_askers);

                    // adding contact to contact list
                    contactList.add(contact);
                }
            } catch (final JSONException e) {
                //Log.e(TAG, "Json parsing error: " + e.getMessage());                    
            }
        } else {}
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        toast(pre_phone);
    }
}

有誰知道我該怎么做?

您需要重新考慮一下您的設計以尊重活動生命周期。 根據定義,您不應該從暫停的 Activity 在 UI 中執行任何操作。

另外,AsyncTask 會給你帶來很多麻煩——我建議你完全遠離它,尋找更好的方法進行后台處理。 我建議后台服務。

您還應該認真考慮擺脫進度對話框和吐司。 你這里有一個后台任務,它實際上沒有連接到 UI 的任何部分,試圖讓它連接到 UI 可能會給你帶來無窮無盡的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM