如何在Android中与正在运行的线程通信服务

时间:2012-01-24 15:08:02

标签: android multithreading service communication

我的目标是午餐服务,以满足所有应用程序网络的需求。

我想可能打开2个插座进行数据传输。我希望数据是异步处理的,所以我认为我应该在两个独立的线程中运行它们,每个线程对应每个套接字,这样数据就可以在两个不同的“链接”异步中流式传输。

所以,我会很感激两件事:

  1. 更好的整体设计。也许我完全弄错了..

  2. 有人可以向我解释,一旦我需要将数据传入/传出主服务,我该如何与这些传播进行通信?据我所知(操作系统)我需要使用SIGNALS :)(开个玩笑..)

2 个答案:

答案 0 :(得分:2)

嗯,我可以说我终于得到了它,就在放弃之前。这是一个超级简单的应用程序,它在一个活动中运行一个线程,并为每个实体使用两个不同的处理程序处理双向通信!

代码:

public class MainActivity extends Activity  {   
//Properties:   
    private final   String TAG = "Activity";            //Log tag
    private         MyThread mThread;                   //spawned thread 
    Bundle          myB = new Bundle();                 //used for creating the msgs
    public          Handler mHandler = new Handler(){   //handles the INcoming msgs 
        @Override public void handleMessage(Message msg) 
        { 
            myB = msg.getData();
            Log.i(TAG, "Handler got message"+ myB.getInt("THREAD DELIVERY")); 
        } 
    }; 
//Methods:
    //--------------------------
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        mThread = new MyThread(mHandler);
        mThread.start();
        sendMsgToThread();
    }
    //-------------------------- 
    void sendMsgToThread() 
    { 
        Message msg = mThread.getHandler().obtainMessage(); 
        myB.putInt("MAIN DELIVERY", 321);
        msg.setData(myB);
        mThread.getHandler().sendMessage(msg);
    } 
}
//=========================================================================================
//=========================================================================================

public class MyThread extends Thread{   
//Properties:
    private final   String TAG = "MyThread";            //Log tag
    private         Handler outHandler;                 //handles the OUTgoing msgs 
    Bundle          myB = new Bundle();                 //used for creating the msgs
    private         Handler inHandler = new Handler(){  //handles the INcoming msgs 
        @Override public void handleMessage(Message msg) 
        { 
            myB = msg.getData();
            Log.i(TAG, "Handler got message"+ myB.getInt("MAIN DELIVERY")); 
        } 
    }; 

//Methods:
    //--------------------------
    public void run(){
        sendMsgToMainThread();  //send to the main activity a msg
        Looper.prepare();
        Looper.loop();
        //after this line nothing happens because of the LOOP!
        Log.i(TAG, "Lost message");
    }
    //--------------------------
    public MyThread(Handler mHandler) {
        //C-tor that get a reference object to the MainActivity handler.
        //this is how we know to whom we need to connect with.
        outHandler = mHandler;
    }
    //--------------------------
    public Handler getHandler(){
        //a Get method which return the handler which This Thread is connected with.
        return inHandler;
    }
    //--------------------------    
    private void sendMsgToMainThread(){
        Message msg = outHandler.obtainMessage();   
        myB.putInt("THREAD DELIVERY", 123);
        msg.setData(myB);
        outHandler.sendMessage(msg);
    }
}
//=========================================================================================
//=========================================================================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="test.test.namespace"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

输出是:

01-26 06:25:40.683: I/Activity(19560): Handler got message123
01-26 06:25:40.683: I/MyThread(19560): Handler got message321

我在阅读由endian here提供的帖子时想到了这一点。

我希望其他人会觉得这很有用。祝你好运:)

答案 1 :(得分:0)

Here是使用处理程序解释线程和通信的好帖子。此外,同一博客中有许多关于Android中各种线程构造的帖子。

另一种可能性是使用AsyncTasks。找一些解释here