在android中每2秒获取一次tcp连接

在android中每2秒获取一次tcp连接,android,tcp,refresh,Android,Tcp,Refresh,我有一个获取tcp连接列表的类,我获取这个列表并在ListView中显示它。 我想每2-3秒刷新此列表视图并显示新连接(连接监控) //GetTcpConnections.java package Core; import android.content.Context; import android.os.AsyncTask; import android.os.CountDownTimer;

我有一个获取tcp连接列表的类,我获取这个列表并在ListView中显示它。 我想每2-3秒刷新此列表视图并显示新连接(连接监控)

        //GetTcpConnections.java

        package Core;

        import android.content.Context;
        import android.os.AsyncTask;
        import android.os.CountDownTimer;
        import android.os.Handler;
        import android.util.Log;

        import java.io.BufferedReader;
        import java.io.File;
        import java.io.FileReader;
        import java.util.ArrayList;
        import java.util.Timer;
        import java.util.TimerTask;

        import Adapters.ConnectionsAdapter;
        import Structs.TcpConnectionData;
        import Utils.TcpUtils;



        public class GetTcpConnection {

           Context context;

            private int interval = 2;

            private TcpConnectionLitener litener = null;

            public GetTcpConnection(Context ctx,TcpConnectionLitener lst){
                context = ctx;
                litener = lst;
            }




            public interface TcpConnectionLitener{
                public void onRecvData(ArrayList<TcpConnectionData> result);
            }


            private void waitTime(int sec) {
                try {
                    // sleep
                    Thread.sleep(1000*sec);
                } catch (InterruptedException e) { }
            }

            class task extends AsyncTask <GetTcpConnection,Void,ArrayList<TcpConnectionData>>{





                @Override
                protected ArrayList<TcpConnectionData> doInBackground(GetTcpConnection... data) {



                    waitTime(2);



                    return getConnections();


                }

                @Override
                protected void onPostExecute(ArrayList<TcpConnectionData> tcpConnectionDatas) {
                    super.onPostExecute(tcpConnectionDatas);


                    litener.onRecvData(tcpConnectionDatas);



                }
            }


            public void startQue() {


        /*  TimerTask  have error

                 TimerTask timerTask = new TimerTask() {

                    @Override
                    public void run() {

                        new task().execute();
                    }
                };

                Timer timer = new Timer();
                timer.scheduleAtFixedRate(timerTask, 0, interval);
        */


                while(true){
                    waitTime(interval);
                    new task().execute();

                }

                }


            }


        // Get tcp connection list

            public ArrayList<TcpConnectionData> getConnections(){
               ............
               ............

                return  output;

            }
        }
//gettcpcconnections.java
包芯;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.os.CountDownTimer;
导入android.os.Handler;
导入android.util.Log;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.util.ArrayList;
导入java.util.Timer;
导入java.util.TimerTask;
导入适配器。连接适配器;
导入Structs.TcpConnectionData;
导入Utils.TcpUtils;
公共类连接{
语境;
私有整数区间=2;
私有TcpConnectionLitener-litener=null;
公共GetTcpConnection(上下文ctx、TcpConnectionLitener lst){
上下文=ctx;
litener=lst;
}
公共接口TcpConnectionLitener{
在Recvdata(ArrayList结果)上公开无效;
}
专用无效等待时间(整数秒){
试一试{
//睡眠
线程睡眠(1000*秒);
}捕获(中断异常e){}
}
类任务扩展了异步任务{
@凌驾
受保护的ArrayList doInBackground(GetTcpConnection…数据){
等待时间(2);
返回getConnections();
}
@凌驾
受保护的void onPostExecute(ArrayList tcpConnectionDatas){
super.onPostExecute(tcpConnectionDatas);
litener.onRecvData(tcpConnectionDatas);
}
}
公共无效startquee(){
/*TimerTask有错误
TimerTask TimerTask=新TimerTask(){
@凌驾
公开募捐{
新任务().execute();
}
};
定时器=新定时器();
timer.scheduleAtFixedRate(timerTask,0,interval);
*/
while(true){
等待时间(间隔);
新任务().execute();
}
}
}
//获取tcp连接列表
公共ArrayList getConnections(){
............
............
返回输出;
}
}
这是活动包含ui(listview)

///TcpConnectionsActivity.java
包装ir.nancy.parentalcontroller;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.ListView;
导入java.util.ArrayList;
导入适配器。连接适配器;
导入Core.gettcpcconnection;
导入Structs.TcpConnectionData;
导入Core.GetTcpConnection.TcpConnectionLitener;
公共类TcpConnectionActivity扩展了ActionBarActivity实现了TcpConnectionLitener{
私有连接TCP连接;
私有列表视图列表视图;
私有数组列表数据;
专用连接适配器;
私有整数[]项;
@凌驾
公共无效onRecvData(ArrayList结果){
如果(结果==null){
回来
}
如果(数据!=null){
如果(数据等于(结果)){
回来
}
}
数据=结果;
如果(datas.size()>0){
items=新整数[datas.size()];
int i=0;
用于(TCP连接数据_i:datas){
项目[i]=\u i.uid;
}
适配器=新连接适配器(此、项、数据);
setAdapter(适配器);
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u tcp\u连接);
tcpConnection=newgettcpconnection(this,this);
listView=(listView)findViewById(R.id.listView);
tcpConnection.startQue();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u tcp\u连接,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
事实上,问题是内存泄漏(android中的关闭/等待对话框)。 我如何解决它

问题出在哪里???

您在GetTcpConnection类中泄漏了上下文(一个巨大的对象),因为您用sleep方法阻塞了线程。要以指定的间隔执行任务,请使用或ScheduledExecutorService。切勿使用Thread.slee
        /// TcpConnectionsActivity.java
        package ir.nancy.parentalcontroller;

        import android.support.v7.app.ActionBarActivity;
        import android.os.Bundle;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.widget.ListView;

        import java.util.ArrayList;

        import Adapters.ConnectionsAdapter;
        import Core.GetTcpConnection;
        import Structs.TcpConnectionData;

        import Core.GetTcpConnection.TcpConnectionLitener;

        public class TcpConnectionActivity extends ActionBarActivity implements TcpConnectionLitener {

            private GetTcpConnection tcpConnection;
            private ListView listView;

            private ArrayList<TcpConnectionData> datas;



            private ConnectionsAdapter adapter;

            private Integer[] items;


            @Override
            public void onRecvData(ArrayList<TcpConnectionData> result) {
                if (result == null){
                    return;
                }

                if (datas != null){
                    if (datas.equals(result)){
                        return;
                    }
                }

                datas = result;
                if (datas.size() > 0) {
                    items = new Integer[datas.size()];

                    int i = 0;

                    for (TcpConnectionData _i : datas) {
                        items[i] = _i.uid;
                    }

                    adapter = new ConnectionsAdapter(this, items, datas);

                    listView.setAdapter(adapter);

                }
            }

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_tcp_connection);

                tcpConnection = new GetTcpConnection(this,this);
                listView = (ListView) findViewById(R.id.listView);

                     tcpConnection.startQue();

            }


            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.menu_tcp_connection, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();

                //noinspection SimplifiableIfStatement
                if (id == R.id.action_settings) {
                    return true;
                }

                return super.onOptionsItemSelected(item);
            }
        }