Service是系统开机时,接收到广播后就会启动,作用是获取底层数据。
现在起一个app想从Service中通过Handler方式获取数据,
Activity是通过bindService与Service绑定的,但是Activity一直收不到数据。
测试发现是Service中handler为null。
问题就是Service启动后,还能不能通过Activity来操作设置Service的handler。
activity中主要代码如下:
public class MainActivity extends Activity {
UsbDongleService usb = null;
private Handler mHandler = new Handler() {
public void handleMessage (Message msg) {
switch(msg.what) {
case UsbDongleService.MSG_USB_ATTACH:
Toast.makeText(getApplicationContext(), "USB 插入", Toast.LENGTH_SHORT).show();
break;
case UsbDongleService.MSG_USB_DETACH:
Toast.makeText(getApplicationContext(), "USB 拔出", Toast.LENGTH_SHORT).show();
break;
case UsbDongleService.MSG_USB_RX:
String t = (String)msg.obj;
Toast.makeText(getApplicationContext(), t, Toast.LENGTH_LONG).show();
TextView tv = (TextView)findViewById(R.id.textView2);
tv.setText(t);
break;
}
}
}; private ServiceConnection connection = new ServiceConnection(){ @Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
usb = new UsbDongleService();
usb.setHander(mHandler);
Log.d("connection","onConnected");
} @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
usb = null;
Log.d("connection", "onDisconnected");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setClass(MainActivity.this, UsbDongleService.class);
bindService(intent,connection,Context.BIND_AUTO_CREATE);
}
现在起一个app想从Service中通过Handler方式获取数据,
Activity是通过bindService与Service绑定的,但是Activity一直收不到数据。
测试发现是Service中handler为null。
问题就是Service启动后,还能不能通过Activity来操作设置Service的handler。
activity中主要代码如下:
public class MainActivity extends Activity {
UsbDongleService usb = null;
private Handler mHandler = new Handler() {
public void handleMessage (Message msg) {
switch(msg.what) {
case UsbDongleService.MSG_USB_ATTACH:
Toast.makeText(getApplicationContext(), "USB 插入", Toast.LENGTH_SHORT).show();
break;
case UsbDongleService.MSG_USB_DETACH:
Toast.makeText(getApplicationContext(), "USB 拔出", Toast.LENGTH_SHORT).show();
break;
case UsbDongleService.MSG_USB_RX:
String t = (String)msg.obj;
Toast.makeText(getApplicationContext(), t, Toast.LENGTH_LONG).show();
TextView tv = (TextView)findViewById(R.id.textView2);
tv.setText(t);
break;
}
}
}; private ServiceConnection connection = new ServiceConnection(){ @Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
usb = new UsbDongleService();
usb.setHander(mHandler);
Log.d("connection","onConnected");
} @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
usb = null;
Log.d("connection", "onDisconnected");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setClass(MainActivity.this, UsbDongleService.class);
bindService(intent,connection,Context.BIND_AUTO_CREATE);
}
我建议你用广播,在Activity里面注册一个广播,然后在Service里面发送广播,这个是最常用的方法来把Service获取的数据 传给UI层。usb = new UsbDongleService();
usb.setHander(mHandler);
这样设置显然Service是无法收到mHandler对象的,UsbDongleService这个无需实例化,Android 系统会实例化的。
应该通过Service里面的Binder子类去设置一个对象。
我用的Messenger解决的,就是Binder子类吧?
因为这Service和Activity是在一个apk中的,我还觉得这算不算进程之间的通信。
跨进程 可以 有 Intent 广播 ,contentProvider 以及 AIDL