问题描述
我的目标是午餐会照顾所有的网络应用需求的服务。
我想也许开2个插槽进行数据传输。我想对数据进行异步处理,所以我想我被我应该在两个独立的线程中运行它们,每次每一个插座上,这样的数据可以在两个不同的链接被流异步..
,我就AP preciate两件事情:
-
一个更好的整体设计。也许我完全完全搞错了。
-
可有人向我解释,我怎么能与那些threds沟通一次,我需要从他们的主要业务数据传递到/到/?据我了解到(OS)我需要使用信号:)(开个玩笑。)
好了,我可以说我终于得到它,就放弃了。这里是一个超级骗子运行活动中的一个线程,处理一个双向通信使用的两种不同的处理程序为每个实体非常简单的应用程序!
在code:
公共类MainActivity延伸活动{
//属性:
私人最终字符串变量=活动; //日志标签
私人MyThread mThread; //衍生的线程
捆绑MYB =新包(); //用于创建封邮件
公开处理程序mHandler =新的处理程序(){//处理传入封邮件
@覆盖公共无效的handleMessage(信息MSG)
{
MYB = msg.getData();
Log.i(TAG处理器得到了消息+ myB.getInt(送线));
}
};
//方法:
// --------------------------
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
mThread =新MyThread(mHandler);
mThread.start();
sendMsgToThread();
}
// --------------------------
无效sendMsgToThread()
{
消息味精= mThread.getHandler()obtainMessage()。
myB.putInt(主传送,321);
msg.setData(MYB);
。mThread.getHandler()的sendMessage(MSG);
}
}
// ================================================ =========================================
// ================================================ =========================================
公共类MyThread继承Thread {
//属性:
私人最终字符串变量=MyThread; //日志标签
私人处理器outHandler; //处理传出的MSG
捆绑MYB =新包(); //用于创建封邮件
私人处理程序inHandler =新的处理程序(){//处理传入封邮件
@覆盖公共无效的handleMessage(信息MSG)
{
MYB = msg.getData();
Log.i(TAG处理器得到了消息+ myB.getInt(主传送));
}
};
//方法:
// --------------------------
公共无效的run(){
sendMsgToMainThread(); //发送到主活性的味精
活套prepare()。
Looper.loop();
//这一行没有之后发生的,因为在循环!
Log.i(TAG,失落的消息);
}
// --------------------------
公共MyThread(处理器mHandler){
// C-器是获得一个参考对象的MainActivity处理程序。
//这是我们怎么知道向谁,我们需要用连接。
outHandler = mHandler;
}
// --------------------------
公共处理器getHandler(){
//一个Get方法,它返回的这个主题是与连接处理程序。
返回inHandler;
}
// --------------------------
私人无效sendMsgToMainThread(){
消息味精= outHandler.obtainMessage();
myB.putInt(送线,123);
msg.setData(MYB);
outHandler.sendMessage(MSG);
}
}
// ================================================ =========================================
// ================================================ =========================================
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=test.test.namespace
安卓版code =1
机器人:VERSIONNAME =1.0>
<使用-SDK安卓的minSdkVersion =10/>
<应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>
<活动机器人:MainActivity名称=机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>
< /舱单>
输出:
6月一号至26日:25:40.683:I /活动(19560):处理器得到了message123
6月一号至26日:25:40.683:I / MyThread(19560):处理器得到了message321
我想通了这一点,在读取端所提供的职位,的。
我希望其他人会发现这很有用。好运气:)
my goal is to lunch a service that will take care for all of the application network needs.
i thought maybe to open 2 sockets for data transfer. i want the data to be handled asynchronously, so i was thinking my be i should run them in two separated threads, each for every socket, and that way the data could be streamed in two different "links" async..
so, i would appreciate two things:
a better overall design. maybe i completely got it all wrong..
can someone explain to me how can i communicate with those threds once i need to pass data to/from them to/from the main service? as far as i learnt (OS) i need to use SIGNALS:) (just kidding..)
Well, I can say i finally got it, just before giving up. Here is a super duper extremely simple app that runs a thread inside an activity and handles a bi-directional communication using two different Handlers for each entity!
The code:
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>
the output is:
01-26 06:25:40.683: I/Activity(19560): Handler got message123
01-26 06:25:40.683: I/MyThread(19560): Handler got message321
I figured this out while reading the offered post by endian, here.
I hope others will find this useful. good luck:)
这篇关于如何在Android的一个正在运行的线程通信的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!