千本摩的
2015-05-23 01:53
采纳率: 66.7%
浏览 7.0k
已采纳

这个activity finish 后 为什么内存没被回收 ?谁阻止了回收?!!!

public class LoginActivity extends FragmentActivity {

    UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");
    @InjectView(R.id.photo_welcome)
    RoundedImageView photoWelcome;
    @InjectView(R.id.photo_welcome_con)
    FrameLayout photoWelcomeCon;
    @InjectView(R.id.photo_slogan)
    ImageView photoSlogan;
    @InjectView(R.id.login_type_title)
    TextView loginTypeTitle;
    @InjectView(R.id.login_type_title_con)
    FrameLayout loginTypeTitleCon;
    @InjectView(R.id.btn_mobile)
    FloatingActionButton btnMobile;
    @InjectView(R.id.btn_wechat)
    FloatingActionButton btnWechat;
    @InjectView(R.id.btn_sina)
    FloatingActionButton btnSina;
    @InjectView(R.id.btn_qq)
    FloatingActionButton btnQq;
    @InjectView(R.id.action_con)
    LinearLayout actionCon;
    @InjectView(R.id.real_con)
    LinearLayout realCon;

    private Activity mContext;
    private RoundProgressDialog nProgress;
    private SHARE_MEDIA[] mPlatformsMap;
    public String deviceToken = "";
    private Timer tokenTimer;
    private int waitTime = 120;
    private int tokenCount = 0;
    private View.OnLongClickListener toggleServer = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            if (SystemUtils.debug) {
                ApiUtils.mDomain = ApiUtils.testDomain.equalsIgnoreCase(ApiUtils.mDomain) ? ApiUtils.realDomain : ApiUtils.testDomain;
                BuyueApp.get().setTestMode(ApiUtils.testDomain.equalsIgnoreCase(ApiUtils.mDomain));

                SystemUtils.showMsg("change to: \n " + ApiUtils.mDomain, mContext);
                EventDao evtDao = new EventDao(BuyueApp.get().getApplicationContext());
                BoardDao boardDao = new BoardDao(BuyueApp.get().getApplicationContext());
                CardDao cardDao = new CardDao(BuyueApp.get().getApplicationContext());
                SyncDao syncDao = new SyncDao(BuyueApp.get().getApplicationContext());
                syncDao.markDirty(Sync.EVENT);
                syncDao.markDirty(Sync.BOARD);
                try {
                    evtDao.deleteAll();
                    boardDao.deleteAll();
                    cardDao.deleteAll();
                } catch (SQLException e) {
                    SystemUtils.attemptPrintError(e);
                }
            }
            return false;
        }
    };

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = LoginActivity.this;
        BuyueApp.get().addActivity(LoginActivity.this);

        setContentView(R.layout.activity_login);
        ButterKnife.inject(this);
        //首次执行导入.db文件
        DataManager dbHelper = new DataManager(this);
        dbHelper.openDatabase();
        dbHelper.closeDatabase();

//        umeng message
        PushAgent mPushAgent = PushAgent.getInstance(mContext);
        mPushAgent.enable();
//        umeng analytics
        MobclickAgent.updateOnlineConfig(mContext);
//        umeng upadate
        UmengUpdateAgent.update(this);
        UmengUpdateAgent.setUpdateOnlyWifi(false);
        deviceToken = "";
        tokenTimer = new Timer();

        TimerTask tokenTask = new TimerTask() {
            @Override
            public void run() {

                if (deviceToken != null && !"".equalsIgnoreCase(deviceToken)) {
                    SpUtils.setSpStr(SpUtils.TEP_TOKEN,deviceToken);
                    tokenTimer.cancel();
                } else {
                    deviceToken = UmengRegistrar.getRegistrationId(mContext);
                    tokenCount++;
                }
                if (tokenCount > 300) {
                    tokenTimer.cancel();
                }
            }
        };
        tokenTimer.schedule(tokenTask, 0, 100);

        PushAgent.getInstance(mContext).onAppStart();
        FileUtils.deleteSave();
        FileUtils.clearCache();
        regCasts();
        adjustMyLayout();
        initLogin();
        TextView typeTitle = (TextView) findViewById(R.id.login_type_title);
        typeTitle.setOnLongClickListener(toggleServer);
    }

    private void adjustMyLayout() {

    }

    private void regCasts() {
        SystemUtils.registerReceiver(receiver, NameUitls.ACTION_DO_LOGIN, mContext);
        SystemUtils.registerReceiver(finishReceiver, NameUitls.ACTION_LOGOUT, mContext);
        SystemUtils.registerReceiver(timeOutReceiver, NameUitls.ACTION_TIMEOUT, mContext);
    }

    private void unRegCasts() {
        SystemUtils.unregisterReceiver(receiver, mContext);
        SystemUtils.unregisterReceiver(finishReceiver, mContext);
        SystemUtils.unregisterReceiver(timeOutReceiver, mContext);
    }


    private void initPlatformMap() {
        mPlatformsMap = new SHARE_MEDIA[]{SHARE_MEDIA.GOOGLEPLUS, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ};
    }



  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 千本摩的 2015-05-28 05:04
    已采纳

    没人回答么!!!!!!!

    点赞 打赏 评论
  • 千本摩的 2015-05-23 01:56
     private void initLogin() {
    
            initPlatformMap();
            LinearLayout actionCon = (LinearLayout) findViewById(R.id.action_con);
            int len = actionCon.getChildCount();
    
            String appId = Config.WX_APPID;
            String appSecret = Config.WX_APPSERCRET;
            UMWXHandler wxHandler = new UMWXHandler(LoginActivity.this, appId, appSecret);
            wxHandler.addToSocialSDK();
            mController.getConfig().setSsoHandler(new SinaSsoHandler());
            String qqId = Config.QQ_APPID;
            String qqKey = Config.QQ_APPKEY;
            UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(LoginActivity.this, qqId, qqKey);
            qqSsoHandler.addToSocialSDK();
            mController.getConfig().addFollow(SHARE_MEDIA.SINA, Config.SINA_ID);
            btnMobile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.setEnabled(false);
                    Intent intent = new Intent();
                    intent.putExtra("umtoken",deviceToken);
                    intent.setClass(LoginActivity.this, SignMainActivity.class);
                    startActivity(intent);
    //                NameUitls aboutUs = NameUitls.newInstance();
    //                FragmentManager fm = getFragmentManager();
    //                FragmentTransaction ft = fm.beginTransaction();
    //                aboutUs.show(ft, FRAG_SIGN_MAIN);
                    v.setEnabled(true);
                }
            });
            btnMobile.setOnTouchListener(SystemUtils.TouchDark);
            for (int i = 1; i < len; i++) {
                FloatingActionButton btn = (FloatingActionButton) ((FrameLayout) actionCon.getChildAt(i)).getChildAt(0);
                final int finalI = i;
                final SHARE_MEDIA path = mPlatformsMap[finalI];
                btn.setOnTouchListener(SystemUtils.TouchDark);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
    
                        disableBtns();
                        if (SystemUtils.isUnConnected(mContext)) {
                            SystemUtils.showMsg(mContext.getString(R.string.msg_net_timeout), mContext);
                            enableBtns();
                            return;
                        }
                        attemptShowProgress("登录中", waitTime);
                        mController.doOauthVerify(LoginActivity.this, path, new SocializeListeners.UMAuthListener() {
    
                            @Override
                            public void onComplete(final Bundle value, SHARE_MEDIA share_media) {
    //                            progressDialog = ToolUtils.showRoundProgress("登录中", mContext);
                                if (value != null && !TextUtils.isEmpty(value.getString("uid"))) {
                                    mController.getPlatformInfo(LoginActivity.this, path, new SocializeListeners.UMDataListener() {
                                        @Override
                                        public void onComplete(int status, Map<String, Object> info) {
                                            if (status == 200 && info != null) {
                                                User user = null;
                                                try {
                                                    switch (finalI - 1) {
                                                        case 0:
                                                            user = SdkUtils.getUserFromWechat(info, deviceToken);
                                                            break;
                                                        case 1:
                                                            user = SdkUtils.getUserFromWeibo(info, deviceToken);
                                                            break;
                                                        case 2:
                                                            String puid = value.getString("uid");
                                                            user = SdkUtils.getUserFromQq(info, puid, deviceToken);
                                                            break;
                                                    }
                                                    Boolean result = DataCenter.login(user, mContext);
                                                    dealLogin(result);
                                                } catch (NullPointerException e) {
                                                    dealLogin(false);
                                                }
                                            } else {
                                                prepareLogin();
                                            }
                                        }
    
                                        @Override
                                        public void onStart() {
    
                                        }
                                    });
                                } else {
                                    prepareLogin();
                                    SystemUtils.showMsg("授权失败,请稍后重试", mContext);
                                }
                            }
    
                            @Override
                            public void onStart(SHARE_MEDIA share_media) {
                            }
    
                            @Override
                            public void onError(SocializeException e, SHARE_MEDIA share_media) {
                                prepareLogin();
                            }
    
                            @Override
                            public void onCancel(SHARE_MEDIA share_media) {
                                prepareLogin();
                            }
                        });
                    }
                });
            }
            if (BuyueApp.get().authed()) {
                DataCenter.login(BuyueApp.get().getUser(), mContext, true);
                Intent toMain = new Intent();
                toMain.setClass(mContext, MainActivity.class);
                toMain.putExtra(NameUitls.NEED_LOGIN_XMPP, true);
                LoginActivity.this.startActivity(toMain);
                LoginActivity.this.finish();
            } else {
                if (BuyueApp.get().getUser() != null && DataUtils.notZero(String.valueOf(BuyueApp.get().getUser().getId()))) {
                    attemptShowProgress("登录中", 30);
                    boolean result = DataCenter.login(BuyueApp.get().getUser(), mContext);
                    dealLogin(result);
                }
            }
        }
    
        public void dealLogin(boolean result) {
            if (result == Boolean.TRUE) {
                attemptShowProgress("登录中", waitTime);
            } else {
                SystemUtils.dismissDialog(nProgress);
                SystemUtils.showMsg(R.string.msg_net_timeout, mContext);
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
            if (ssoHandler != null) {
                ssoHandler.authorizeCallBack(requestCode, resultCode, data);
            }
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                BuyueApp.get().onTerminate();
            }
            return super.onKeyDown(keyCode, event);
        }
    
        private BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                SystemUtils.dismissDialog(nProgress);
                String ret = intent.getStringExtra("ret");
                String data = intent.getStringExtra("data");
                if (ApiUtils.RET_S.equalsIgnoreCase(ret)) {
                    Intent toMain = new Intent();
                    toMain.setClass(mContext, MainActivity.class);
                    toMain.putExtra(NameUitls.NEED_LOGIN_XMPP, true);
                    LoginActivity.this.startActivity(toMain);
                    LoginActivity.this.finish();
                } else {
                    SystemUtils.showMsg(data, mContext);
                    enableBtns();
                }
            }
        };
    
        private BroadcastReceiver finishReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                SystemUtils.dismissDialog(nProgress);
            }
        };
    
        private BroadcastReceiver timeOutReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                SystemUtils.dismissDialog(nProgress);
                SystemUtils.showMsg(R.string.msg_net_timeout, mContext);
                enableBtns();
            }
        };
    
        @Override
        protected void onResume() {
            enableBtns();
            tokenCount = 0;
            super.onResume();
    //        SystemUtils.bindLocation(mContext);
            MobclickAgent.onResume(this);
        }
    
        @Override
        public void onPause() {
            super.onPause();
    //        SystemUtils.unbindLocation(mContext);
            MobclickAgent.onPause(this);
        }
    
        @Override
        protected void onNewIntent(Intent intent) {
            enableBtns();
        }
    
        @Override
        protected void onDestroy() {
            SystemUtils.dismissDialog(nProgress);
            unRegCasts();
    
            BuyueApp.get().removeActivity(LoginActivity.this);
            super.onDestroy();
            SystemUtils.debugLog("test-cache","login destroy",BuyueApp.get().getActivitys()+"");
        }
    
        private void disableBtns() {
            btnQq.setEnabled(false);
            btnWechat.setEnabled(false);
            btnSina.setEnabled(false);
        }
    
        private void enableBtns() {
            btnQq.setEnabled(true);
            btnWechat.setEnabled(true);
            btnSina.setEnabled(true);
        }
    
        private void prepareLogin() {
            enableBtns();
            SystemUtils.dismissDialog(nProgress);
        }
    
        private void attemptShowProgress(String msg, int waitTime) {
            SystemUtils.dismissDialog(nProgress);
            nProgress = ToolUtils.showRoundProgress(msg, mContext, waitTime);
        }
    
    点赞 打赏 评论
  • tracymac7 2015-07-29 17:02

    夜深了 代码没看
    看了题目
    activity在创建界面的时候,由setContentView创建一个主view(即绘制整个界面的父view),由于在finish的时候,这个view还持有activity的引用,因此activity无法被回收,造成内存泄漏
    如果是这种情况,可以找我探讨一下~

    点赞 打赏 评论
  • qq_31410925 2016-05-23 12:13

    大神,我也遇到同样的问题,求帮助qq 654897348

    点赞 打赏 评论

相关推荐 更多相似问题