简体   繁体   English

当活动处于 onPause 生命周期时尝试运行 aynktask

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

I need to make a notification when my DB gets change and I cant use firebase in this project so I try to make a solution here : (if I make asynktask for check DB and it repeats periodically then the problem is solved) but I have crush when I try to run asynktask when activity is in onPause() situation, it doesn't work!!!我需要在我的数据库发生变化时发出通知,我不能在这个项目中使用 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);
    }
}

Does anyone have an idea how can I do this?有谁知道我该怎么做?

You'll need to rethink your design a bit to respect the Activity Lifecycle.您需要重新考虑一下您的设计以尊重活动生命周期。 By definition, you shouldn't be doing anything in the UI from a paused activity.根据定义,您不应该从暂停的 Activity 在 UI 中执行任何操作。

Also, AsyncTask is going to give you a lot of trouble - I suggest that you stay away from it completely and find a better way to do background processing.另外,AsyncTask 会给你带来很多麻烦——我建议你完全远离它,寻找更好的方法进行后台处理。 I suggest a background service.我建议后台服务。

You should also seriously consider getting rid of the progress dialog and the toast.您还应该认真考虑摆脱进度对话框和吐司。 What you have here is a background task that's really not connected to any piece of the UI, and trying to keep it connected to the UI is probably going to cause you endless problems.你这里有一个后台任务,它实际上没有连接到 UI 的任何部分,试图让它连接到 UI 可能会给你带来无穷无尽的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM