[英]How to pass Object of Service to MyActivity?
First of all: I am bigger in Android programming
首先:我在Android编程方面做得更好
I have created simple TCP Socket using Service. 我已经使用Service创建了简单的TCP套接字。
The client connects to server when the button in MainActivity
is triggered. 触发
MainActivity
的按钮时,客户端将连接到服务器。
Now i want to create PrintWriter
object in MainActivity
using Socket
object of Service. 现在,我想使用Service的
Socket
对象在MainActivity
创建PrintWriter
对象。
I got to know it is possible to pass objects from Service
to Activity
using JSON. 我知道可以使用JSON将对象从
Service
传递到Activity
。
I searched over Internet but couldn't get an idea how I can pass it. 我通过互联网搜索,但不知道该如何通过。
Please give me simple code example to pass the object. 请给我简单的代码示例以传递对象。
MyService.java MyService.java
public class MyService extends Service{
public static Socket clientsocket;
public static PrintWriter printer;
SendMessage sender;
@Override
public void onCreate() {
Toast.makeText(this, "Created", Toast.LENGTH_LONG).show();
super.onCreate();
}
@Override
public void onDestroy() {
Toast.makeText(this, "Stoped", Toast.LENGTH_LONG).show();
if(clientsocket!=null){
try{
clientsocket.close();
Toast.makeText(this, "Socket Closed", Toast.LENGTH_LONG).show();
}catch(IOException e){
e.printStackTrace();
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Running", Toast.LENGTH_LONG).show();
SendMessage sender=new SendMessage();
sender.execute();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
class SendMessage extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
try {
clientsocket = new Socket("192.168.237.1", 6666);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if(clientsocket!=null){
Toast.makeText(MyService.this, "Connected", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MyService.this, "Lost Connection", Toast.LENGTH_LONG).show();
}
super.onPostExecute(result);
}
}}
MainActivity.java MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View v){
startService(new Intent(this,MyService.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class PrintMessage extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0) {
return null;
}
}
@Override
protected void onDestroy() {
stopService(new Intent(this,MyService.class));
super.onDestroy();
}}
Now I need a clear & complete code for both MyService.java (how to pass object Ie in this case
Socket
object clientsocket
)and MainActivity.java (how to retreive passed object Ie clientsocket
and use it to create PrintWriter
object) 现在,我需要为MyService.java(在这种情况下如何传递对象
Socket
对象clientsocket
)和MainActivity.java(如何取回传递的对象ie clientsocket
并使用它来创建PrintWriter
对象)的代码都清晰而完整
请考虑使用事件总线库,例如greenrobot EventBus :-)当涉及到在应用程序的各个元素之间传递数据时,这些工作很奇怪,并且使用起来非常安全。
I personally would use the following solution How to send event from Service to Activity with Otto event bus? 我个人将使用以下解决方案如何使用Otto事件总线将事件从服务发送到活动? with Otto Event Bus, and just send an instance of the class with the data in it.
使用Otto Event Bus,并仅发送包含数据的类的实例。
public class MainActivity extends ActionBarActivity
{
@Override
protected void onResume()
{
super.onResume();
SingletonBus.INSTANCE.getBus().register(this);
}
@Override
protected void onPause()
{
SingletonBus.INSTANCE.getBus().unregister(this);
super.onPause();
}
...
}
And 和
// you might need to change this as linked in the above link:
// https://stackoverflow.com/questions/15431768/how-to-send-event-from-service-to-activity-with-otto-event-bus
public enum SingletonBus
{
INSTANCE;
private Bus bus;
private SingletonBus()
{
this.bus = new Bus(ThreadEnforcer.ANY);
}
public Bus getBus()
{
return bus;
}
}
and 和
public class MyEvent
{
private Data data;
public MyEvent(Data data)
{
this.data = data;
}
public Data getData()
{
return data;
}
}
and 和
SingletonBus.INSTANCE.getBus().post(new MyEvent(data));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.