在AsyncTask的doInBackground中没有调用FirebaseMessagingService

时间:2017-09-22 21:52:15

标签: java android multithreading android-asynctask

我有推送通知,我想在手机收到通知时更新领域对象,但是当我尝试启动时:

RealmModelActiveUser actUser= realm.where(RealmModelActiveUser.class).equalTo("id",1).findFirst();
                int myid= actUser.getUser().getUser_id();
                new ServerBackgroundDownloadConversations(getApplicationContext()) {
                    @Override
                    protected void onPostExecute(String result) {
                        super.onPostExecute(result);

                        if (!result.equals("Error")) {
                            Log.i("Conversation", "UPDATED");
                        }
                    }
                }.execute(myid);

程序跳转到构造函数ServerBackgroundDownloadConversations(getApplicationContext())但不调用doInBackground而我不知道原因。

我的AsyncTask:

public class ServerBackgroundCreateConversation extends AsyncTask<RealmModelConversations,Void,String> {
    Context context;

    Handler handler;
    String out= "";
    @SuppressLint("HandlerLeak")
    public ServerBackgroundCreateConversation(Context context) {
        this.context = context;

        handler =  new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Bundle bundle= msg.getData();
                if (bundle!=null){
                    out = (String) bundle.get("response");
                } else {
                    out= "Error";
                }
            }

        };
    }


    @Override
    protected String doInBackground(RealmModelConversations... params) {
        RealmModelConversations newConv = params[0];
            UploadImageApacheHttp uploadTask = new UploadImageApacheHttp();
            uploadTask.doFileUpload(newConv.getWork(newConv.getIntWork()), newConv, handler);


            while (out.equals("")){
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        return out;
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onPostExecute(String result) {

        if (!result.equals("]") || !result.equals("")){
            /// prihlási nového user aj do active (login/register)
            CreateNewConversation(result);
        } else {
            result="Error";
        }

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }


    private void CreateNewConversation(String result){
        Realm realm= Realm.getDefaultInstance();
        try {
            Gson gson = new Gson();
            Type typeConv = new TypeToken<JsonSablonaConversations>() {
            }.getType();

            JSONObject pom;
            JSONArray parentArray = new JSONArray(result);
            JSONObject finalObject = parentArray.getJSONObject(0);

            JsonSablonaConversations conversation = gson.fromJson(finalObject.toString(), typeConv);
            final RealmModelConversations NewUserConv = new RealmModelConversations();
            NewUserConv.setId_dialog(conversation.getId_dialog());
            NewUserConv.setDate(conversation.getDate());
            NewUserConv.setKey(conversation.getKey());
            NewUserConv.setId_user(conversation.getId_user());
            NewUserConv.setId_user2(conversation.getId_user2());
            NewUserConv.setMeno(conversation.getMeno());
            NewUserConv.setMeno2(conversation.getMeno2());
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    try {
                        realm.copyToRealmOrUpdate(NewUserConv);
                    } catch (Exception e) {
                        int pom=4;
                    }
                    RealmResults<RealmModelConversations> ru= realm.where(RealmModelConversations.class).findAll();
                }
            });
        }

        catch (Exception e) {
            int ppp=4;
            ppp++;
        }finally {
            realm.close();
        }
    }
}

我尝试从一个从服务调用的外部线程调用这个↑,但我的AsyncTask有处理程序和处理程序需要在runOnUIthread和Thread中。我无法获取Activity,因为该线程是从无法访问Activity的服务中调用的。

1 个答案:

答案 0 :(得分:0)

我用这段代码解决了我的问题

    public String postData(int myUserId) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.gallopshop.eu/OFY/getConversations.php");
    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("id", Integer.toString(myUserId)));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        String responseStr = EntityUtils.toString(response.getEntity());
        return responseStr;
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
    return null;
}

我会试着把这个方法和&amp;在这个方法之后进入简单的线程,但也许它不需要因为它在服务中。

问题 - 我是否将其置于线程中,或者您认为它会影响应用程序的性能?