内存泄漏 - 服务+线程

时间:2015-09-20 14:28:55

标签: android service memory-leaks

我想知道我做的是否正确。我显然是内存泄漏了,但我无法确定哪里 - 我已经提交了一个简化版本,我认为问题所在。 。 。以下代码中是否存在泄漏的可能性?

public class MainActivity extends Activity {

    filterService mServer;

    private void startService() {
       Intent mIntent = new Intent(getApplicationContext(), filterService.class);
       startService(mIntent);
       bindService(mIntent, mConnection, BIND_AUTO_CREATE);
    }

    private void stopService() {
        stopService(new Intent(getApplicationContext(), filterService.class));
        unbindService(mConnection);
        mConnection = null;     
    }

    ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceDisconnected(ComponentName name) {
            mServer = null;
        }

        public void onServiceConnected(ComponentName name, IBinder service) {
            LocalBinder mLocalBinder = (LocalBinder)service;
            mServer = mLocalBinder.getServerInstance();
        }
    };

    public void onDestroy() {
        super.onDestroy();
        stopService();
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService();
    }
}

任何评论都是最有价值的 - 谢谢。

0 个答案:

没有答案