首先:我在Android编程方面做得更好
我使用Service创建了简单的TCP套接字。
当触发MainActivity
中的按钮时,客户端连接到服务器。
现在我想使用PrintWriter
服务对象在MainActivity
中创建Socket
对象。
我知道可以使用JSON将对象从Service
传递到Activity
。
我通过互联网搜索,但无法知道如何通过互联网。
请给我简单的代码示例来传递对象。
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
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();
}}
现在我需要一个明确的&amp; MyService.java的完整代码(如何在这种情况下传递对象Ie Socket
对象clientsocket
)和MainActivity.java(如何检索传递的对象Ie clientsocket
并使用它来创建{ {1}}对象)
答案 0 :(得分:0)
请考虑使用事件总线库,例如greenrobot EventBus :-)这些工作奇迹在应用程序的元素之间传递数据并且使用起来非常安全。
答案 1 :(得分:0)
我个人会将以下解决方案How to send event from Service to Activity with Otto event bus?与 Otto 事件总线一起使用,然后只发送包含数据的类实例。
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();
}
...
}
和
// 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;
}
}
和
public class MyEvent
{
private Data data;
public MyEvent(Data data)
{
this.data = data;
}
public Data getData()
{
return data;
}
}
和
SingletonBus.INSTANCE.getBus().post(new MyEvent(data));