【问题标题】:FirebaseMessagingService is not getting called in AsyncTask's doInBackgroundFirebaseMessagingService 没有在 AsyncTask 的 doInBackground 中被调用
【发布时间】:2017-09-22 21:52:15
【问题描述】:

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

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 和 Thread 中。我无法获取 Activity,因为该线程是从无权访问 Activity 的 Service 调用的。

【问题讨论】:

    标签: java android multithreading android-asynctask


    【解决方案1】:

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

        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;
    }
    

    我会尝试将此方法 & 在此方法之后放入简单的线程中,但也许不需要它,因为它在服务中。

    问题 - 我是将其放入 Thread 中还是您认为它会影响应用程序的性能?

    【讨论】:

      猜你喜欢
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2021-02-22
      相关资源
      最近更新 更多