线程(在类中)和活动之间的Android消息传递

时间:2011-06-22 12:33:41

标签: java android multithreading messaging handler

如果将线程放在类中,我如何在活动和线程之间发送消息?

好的,这是一个更详细的代码:

public class AnActivity extends Activity implements OnClickListener, Callback
{

    Button          b           = null;
    TextView        t           = null;
    MyThread        mThread     = null;
    public Handler  mainHandler = new Handler( this );

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

        b = ( Button ) findViewById( R.id.buttonSEND );

        b.setOnClickListener( this );
        mThread = new MyThread( this );
        mThread.start();
    }

    @Override
    public void onClick( View v )
    {
        Message m = Message.obtain();
        m.what = 4;
        mThread.myThreadHandler.sendMessage( m );
    }

    @Override
    public boolean handleMessage( Message msg )
    {
        Toast.makeText( this, "What= " + Integer.toString( msg.what ), Toast.LENGTH_SHORT ).show();
        return false;
    }
}

以下类不是AnActivity的一部分

public class MyThread extends Thread
{
    public Handler myThreadHandler;
    Activity mainActivity;

    public MyThread ( AnActivity anActivity )
    {
        this.mainActivity = anActivity;     
    }


    @Override
    public void run()
    {
        Looper.prepare();
        myThreadHandler = new Handler()
        {
            // sending back a message immediately after a received one
            public void handleMessage( Message msg )
            {
                Message m = Message.obtain();
                m.what = 10;
                // MY PROBLEM:
                // mainActivity.mainHandler.sendMessage( m ); // mainHandler is not visible
                // mainActivity.sendMessage( m ); // does not working
            }
        };
        Looper.loop();
    }
}

3 个答案:

答案 0 :(得分:1)

这是我的活动:

public class AnActivity extends Activity implements OnClickListener, Callback
{

    Button          b           = null;
    TextView        t           = null;
    MyThread        mThread     = null;
    public Handler  mainHandler = new Handler( this );


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

        b = ( Button ) findViewById( R.id.buttonSEND );

        b.setOnClickListener( this );
        mThread = new MyThread( this );
        mThread.start();
    }


    @Override
    public void onClick( View v )
    {
        Message m = Message.obtain();
        m.what = 4;
        mThread.myThreadHandler.sendMessage( m );
    }


    @Override
    public boolean handleMessage( Message msg )
    {
        Toast.makeText( this, "This is AnActivity. What=" + Integer.toString( msg.what ), Toast.LENGTH_SHORT ).show();
        return false;
    }
}

修改过的帖子: 在构造函数中,我可以存储UI线程的messagehandler。

public class MyThread extends Thread
{
    public Handler  myThreadHandler;
    Activity        mainActivity;
    Handler         mainHandler;
    int             what    = -1;


    public MyThread ( AnActivity anActivity )
    {
        this.mainActivity = anActivity;
        this.mainHandler = anActivity.mainHandler;
    }


    @Override
    public void run()
    {
        Looper.prepare();
        myThreadHandler = new Handler()
        {
            public void handleMessage( Message msg )
            {
                what = msg.what;


                // Writing back to main UI thread
                mainActivity.runOnUiThread( new Runnable()
                {

                    @Override
                    public void run()
                    {
                        Toast.makeText( mainActivity, "This is MyThread. What=" + Integer.toString( what ), Toast.LENGTH_SHORT ).show();

                    }
                } );

                Message m = Message.obtain();
                m.what = 10;
                mainHandler.obtainMessage( 12 ).sendToTarget();
            }
        };
        Looper.loop();
    }
}

答案 1 :(得分:0)

我认为您正在搜索“对话框”,因此您可以向用户显示提醒消息,确认消息等。

有关详细信息,请参阅:http://developer.android.com/reference/android/app/Dialog.html

这是警报对话框中的一个很好的示例:http://www.androidpeople.com/android-alertdialog-example/

来自您的评论代码:

AlertDialog.Builder alt_bld = new AlertDialog.Builder(this).create();     
alt_bld.setMessage("apprika target achieve...");
alt_bld.setCancelable(false);
alt_bld.setPositiveButton("yes", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } });
alt_bld.setNegativeButton("No", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); } }); 
alert.show();

通过传递参数,将该代码放入任何方法中。在正在运行的线程或活动中。

试试这个。

答案 2 :(得分:0)

您可以在类中创建一个构造函数,您可以在其中为活动的实例提供。像这样:

public ConnectClass(Activity ac) {
    activity = ac;
}

在这种情况下,您需要在名为activity的线程类中添加一个类变量。