-1

我的项目中有两个主要课程。第一个是用于创建客户端和服务器之间的连接。第二个是用于在活动之间切换。

第一的:

public class MyActivity extends Activity{
private ListView mList;
private ArrayList<String> arrayList;
private MyCustomAdapter mAdapter;
public TCPClient mTcpClient;



@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);




    boolean flag = getIntent().getBooleanExtra("flag",false);
    arrayList = new ArrayList<String>();

    final EditText editText = (EditText) findViewById(R.id.editText);
    Button send = (Button)findViewById(R.id.send_button);
    Button menu = (Button)findViewById(R.id.button1);


    if (flag == true)
    {
        //relate the listView from java to the one created in xml
        mList = (ListView)findViewById(R.id.list);
        mAdapter = new MyCustomAdapter(this, arrayList);
        mList.setAdapter(mAdapter);

        new connectTask().execute("");

        Intent myIntent = new Intent(MyActivity.this,Menu.class);
        startActivity(myIntent);
    }
    send.setOnClickListener(new View.OnClickListener() {
        //  @Override
        public void onClick(View view) {

            String message = editText.getText().toString();

            //clean the listView to 1 item
            if (message.equals("clean"))
            {
                arrayList.removeAll(arrayList);
                mList.removeAllViewsInLayout();
            }
            //add the text in the arrayList
            arrayList.add("c: " + message);

            //sends the message to the server
            if (mTcpClient != null) {
                mTcpClient.sendMessage(message);
            }

            //refresh the list
            mAdapter.notifyDataSetChanged();
            editText.setText("");
        }
    });

    //change Activity to live screen mode (live)
    menu.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            Intent myIntent = new Intent(MyActivity.this, Menu.class);
            startActivity(myIntent);

        }
    });

}

public class connectTask extends AsyncTask<String,String,TCPClient> {

    @Override
    protected TCPClient doInBackground(String... message) {

        //we create a TCPClient object and
        mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
            // @Override
            //print the message as an Item
            public void messageReceived(String message) {
                //this method calls the onProgressUpdate
                publishProgress(message);
            }
        });
        mTcpClient.run();

        return null;

    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);

        //in the arrayList we add the messaged received from server
        arrayList.add(values[0]);
        // notify the adapter that the data set has changed. This means that new message received
        // from server was added to the list
        mAdapter.notifyDataSetChanged();
    }
}

}

对象TCPClient mTcpClient是我的应用程序中的主要因素。我用它与服务器通信。此外,即使我在活动之间切换,它仍然可以正常运行,因此即使我不在该活动中,我仍然可以从服务器获取信息。

第二:

public class Menu extends Activity
{
    public MyActivity myActivity;
    public TCPClient mtcp;

protected void onCreate(Bundle savedInstanceState, MyActivity myActivity) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);



    ImageView action = (ImageView) findViewById(R.id.imageView1);
    action.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            // here I would like to use mTcpClient object mentioned in the first class
            return false;
        }

    });

}

基本上我需要的是关于如何在第二类中创建对第一类mTcpClient中描述的对象的引用的帮助。

4

1 回答 1

2

你这样做是不对的。如果您想使用 TcpClient 类而不考虑上下文,则它不应与第一个 Activity 相关。你应该做的是使用单例模式:

class TcpClient {

    protected static TcpClient mInstance = null;

    public TcpClient() {
        // your init code...
    }

    public static TcpClient getInstance() {
        if( mInstance == null ) {
            mInstance = new TcpClient();
        }

    return mInstance;
    }

...
}

然后,每当您想使用 TcpClient 时,您只需执行以下操作:

TcpClient client = TcpClient.getInstance();
于 2013-04-02T14:34:34.427 回答