如何从另一个活动获取对象引用[android]

时间:2013-04-02 14:22:53

标签: android reference android-asynctask client-server

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

第一

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

1 个答案:

答案 0 :(得分: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();