输入调度超时ANR

时间:2017-11-13 15:20:18

标签: android android-anr-dialog

我在申请中获得了以下ANR

Input dispatching timed out (Waiting to send key event because the focused window has not finished processing all of the input events that were previously delivered to it. Outbound queue length: 0. Wait queue length: 1.)

日志说明 onActivityStopped 方法

中的问题

这是我在此方法中添加的代码

try {
            boolean foreground = new ForegroundCheckTask().execute(getApplicationContext()).get();
            if(!foreground) {

                AdServerManager.getInstance().incrementImpression(activity, ForSaleDataManager.getInstance().getBannerImpression(),
                        ForSaleDataManager.getInstance().getOfferImpression(), new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                ForSaleDataManager.getInstance().clearOfferImpression();
                                ForSaleDataManager.getInstance().clearBannerImpression();
                            }
                        });

                String lastOffers = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedOffersIds());
                //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_OFFER_IDS.getValue() , lastOffers);
                ForSaleDataManager.getInstance().clearVisitedOffersIds();

                String lastCategories = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedCategoriesIds());
                //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_CATEGORY_IDS.getValue() , lastCategories);
                ForSaleDataManager.getInstance().clearVisitedCategoriesIds();

                String lastSearch = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedSearchTerms());
                //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_SEARCH_TERMS.getValue() , lastSearch);
                ForSaleDataManager.getInstance().clearVisitedSearchTerms();

                // clear landing page
                PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.IS_CATEGORIES_LANDING_SHOWEN, false);
                PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.IS_LISTING_LANDING_SHOWEN, false);
                PhoneUtils.deleteWithPrefixFromSharedPreference(activity, ForSaleConstants.IS_SUB_CATEGORIES_LANDING_SHOWEN_PREFIX, false);
                PhoneUtils.deleteWithPrefixFromSharedPreference(activity, ForSaleConstants.IS_SUB_CATEGORIES_LANDING_SHOWEN, false);


                // reset for last activity
                PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.SET_LAST_ACTIVITY_CALLED, false);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

我的应用程序的系统要求,我必须将一些数据发送到服务器并清除本地数据,所以我已经实现了接口 Application.ActivityLifecycleCallbacks

任何人都可以建议如何在不产生ANR的情况下满足我的系统要求吗?

修改

这是我把它放在AsyncTask之后的代码,它不起作用

@Override
    public void onActivityStopped(final Activity activity) {

        //new AsyncTask<Void, Void, Void>() {

            //@Override
            //protected Void doInBackground(Void... voids) {

                try {
                    boolean foreground = new ForegroundCheckTask().execute(getApplicationContext()).get();
                    if(!foreground) {

                        AdServerManager.getInstance().incrementImpression(activity, ForSaleDataManager.getInstance().getBannerImpression(),
                                ForSaleDataManager.getInstance().getOfferImpression(), new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                        ForSaleDataManager.getInstance().clearOfferImpression();
                                        ForSaleDataManager.getInstance().clearBannerImpression();
                                    }
                                });

                        String lastOffers = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedOffersIds());
                        //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_OFFER_IDS.getValue() , lastOffers);
                        ForSaleDataManager.getInstance().clearVisitedOffersIds();

                        String lastCategories = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedCategoriesIds());
                        //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_CATEGORY_IDS.getValue() , lastCategories);
                        ForSaleDataManager.getInstance().clearVisitedCategoriesIds();

                        String lastSearch = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedSearchTerms());
                        //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_SEARCH_TERMS.getValue() , lastSearch);
                        ForSaleDataManager.getInstance().clearVisitedSearchTerms();

                        // clear landing page
                        PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.IS_CATEGORIES_LANDING_SHOWEN, false);
                        PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.IS_LISTING_LANDING_SHOWEN, false);
                        PhoneUtils.deleteWithPrefixFromSharedPreference(activity, ForSaleConstants.IS_SUB_CATEGORIES_LANDING_SHOWEN_PREFIX, false);
                        PhoneUtils.deleteWithPrefixFromSharedPreference(activity, ForSaleConstants.IS_SUB_CATEGORIES_LANDING_SHOWEN, false);


                        // reset for last activity
                        PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.SET_LAST_ACTIVITY_CALLED, false);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }

                //return null;
        //    }
        //}.execute();


    }

1 个答案:

答案 0 :(得分:0)

将所有逻辑移动到后台线程中。这就是你如何解决代码中的任何ANR。