有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在AsyncTask的doInBackground中未调用java FirebaseMessagingService

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

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,我也不知道为什么

我的任务:

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和线程中。我无法获取活动,因为线程是从一个无法访问活动的服务调用的


共 (0) 个答案