Android内存泄漏 - 内部类

时间:2012-08-16 19:47:19

标签: android memory memory-leaks

在我的Android应用中,我有一个启动画面,然后是登录屏幕。用户登录后,他们会登陆欢迎屏幕。我的应用程序在三星Galaxy S3上崩溃很多,所以我决定在MAT中查看。我在欢迎屏幕上进行了堆转储,它在内存中显示了splash对象。我已经发布了下面的代码,如果有人能告诉我导致内存泄漏的原因,我将非常感激:

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

public class SplashActivity extends CoreActivity implements ConnectionCallBack{

    private static final String TAG = SplashActivity.class.getSimpleName();
    private static final int LOGIN_SCREEN = 0;
    protected static final String SPL = SplashActivity.class.getSimpleName();
    private int SHOW_MESSAGE = 1;
    private Timer timer;
    private TextView connectionNotificationTV;
    private int notificationCount = 0;
    private Handler handler;
    TimerTask timerTask;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);
        Globals.appContext = getApplicationContext();

        connectionNotificationTV = (TextView) findViewById(R.id.splash_screen_connection_notification_TV);

        handler = new Handler(){
            public void handleMessage(Message msg) {
                if(msg.what == SHOW_MESSAGE){
                    if(notificationCount == 0){
                        connectionNotificationTV.setVisibility(View.VISIBLE);
                    }else if(notificationCount == 1){
                        confirmationDialog();
                    }
                    notificationCount++;
                }else if(msg.what == LOGIN_SCREEN){
                    loadNext(LOGIN_SCREEN);
                }
            };
        };
        LocationDisplay location = new LocationDisplay(getApplicationContext());   

        ConnectionCheckThread newConnectionThread = new ConnectionCheckThread(this);
        newConnectionThread.start();

        timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                if(notificationCount == 0){
                    handler.sendEmptyMessage(SHOW_MESSAGE);
                }
            }
        }, 30000);

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Utils.unbindDrawables(findViewById(R.id.root));
    }

    private void setScreenDimension(){
      int  screenHeight = getWindowManager().getDefaultDisplay().getHeight();
       int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
       GeneralSettings.getInstance().setScreenHeight(screenHeight);
       GeneralSettings.getInstance().setScreenWidth(screenWidth);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            System.exit(0);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void confirmationDialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.connection_error_title);
        builder.setMessage(R.string.connection_error_message);

        builder.setPositiveButton(R.string.connection_error_close_btn, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                System.exit(0);
            }
        });

        builder.setCancelable(false);
        builder.show();
    }

    @Override
    public void loadNext(int code) {
        if(code == LOGIN_SCREEN){
            Intent intent = new Intent(SplashActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void loadPrev() {
    }

    @Override
    public void onReceivedConnection(int returnCode) {
        if(returnCode == ConnectionCheckThread.CONNECTION_TIMEOUT ||
           returnCode == ConnectionCheckThread.UNKNOWN_HOST ||
           returnCode == ConnectionCheckThread.UNKNOWN_ERROR){
            handler.sendEmptyMessage(SHOW_MESSAGE);
        }else if(returnCode == ConnectionCheckThread.CONNECTION_SUCCESS){
            handler.sendEmptyMessage(LOGIN_SCREEN);
        }
    }

}

以下是线程类的代码:

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Handler;

public class ConnectionCheckThread extends Thread{

    private static final String TAG = ConnectionCheckThread.class.getSimpleName();
    private ConnectionCallBack callback;
    //private HttpURLConnection http = null;
    //private InputStream is;
    //private boolean isFirstTime = true;
    public static int CONNECTION_TIMEOUT = 200;
    public static int UNKNOWN_HOST = 201;
    public static int UNKNOWN_ERROR = 202;
    public static int CONNECTION_SUCCESS = 203;
    public static int CONNECTION_FAILURE = 204;
//  private int returnCode;
//  private int retryCount = 0;
    Context context;

    public ConnectionCheckThread(Context con){
        this.context = con;
        this.callback = (ConnectionCallBack)con;
    }


    public void run(){
        HttpURLConnection http = null;
        boolean isFirstTime = true;
        String urlString = "http://www.google.com/";
        int returnCode = 0;
        int retryCount = 0;

        do{

            try {
                URL urls = new URL(urlString);

                http = (HttpURLConnection)urls.openConnection();

                int responsecode = http.getResponseCode();
                System.out.println("responsecode = "+responsecode);

                if(isFirstTime){
                    http.setConnectTimeout(30000);
                }else{
                    http.setConnectTimeout(15000);
                }

                http.connect();
                InputStream is = http.getInputStream();

                returnCode = CONNECTION_SUCCESS;
            } catch (java.net.SocketException ex) {
                ex.printStackTrace();
                Utils.log(TAG, "caught SocketException[" + ex.getMessage() + "]");
                returnCode = CONNECTION_TIMEOUT;
            } catch (java.net.SocketTimeoutException ex) { 
                ex.printStackTrace();
                Utils.log(TAG, "caught SocketTimeoutException[" + ex.getMessage() + "]");
                returnCode = CONNECTION_TIMEOUT;
            } catch (java.net.UnknownHostException ex) {
                ex.printStackTrace();
                Utils.log(TAG, "caught UnknownHostException[" + ex.getMessage() + "]");
                returnCode = UNKNOWN_HOST;
            } catch (Exception e) {
                e.printStackTrace();
                Utils.log(TAG, "Exception in WebRequest Thread :" + e.getMessage());
                returnCode = UNKNOWN_ERROR;
            } finally {
                try {
                    if (http != null) {
                        http.disconnect();
                        http = null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                callback.onReceivedConnection(returnCode);
                if(retryCount == 0){
                    isFirstTime = false;
                }
                retryCount++;
            }
        }while(retryCount <= 1 && returnCode != CONNECTION_SUCCESS);
    }

    Handler handler2 = new Handler(){
        public void handleMessage(android.os.Message msg) {
            if(msg.what == CONNECTION_FAILURE){
                AlertDialog.Builder alert = new AlertDialog.Builder(context);
                alert.setTitle(context.getResources().getString(R.string.login_no_network_title));
                alert.setMessage(context.getResources().getString(R.string.login_no_network_content));
                alert.show();
            }

        };
    };
}

我将非常感谢我能帮助你解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

请将Handler设为静态类而不是匿名类。我认为SplashActivity必须由android.os.Message和Handler保存。 Handler保留对封闭的SplashActivity的引用。如果不是问题,请在查询'select * from instanceof android.app.Activity'和'path to GC'后粘贴快照。

相关问题