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);
}
解决方案 »
- 窗口小部件Widget的onUpdate事件appWidgetIds.length=1 ???
- android 两个相邻checkbox 之间间距太大,怎么调整
- 关于android界面布局的问题!!
- android 在线播放视频如何实现?有哪些方式?
- 看不起游戏的人侧目一下
- 为什么我无法启动2个模拟器
- 新人求教!!!如何获得发送邮件的android permission
- 电脑和手机通信socket,电脑客户端第二次接收数据的时候卡住了。。。
- android+http+json 数据交互
- 当调用系统发短信界面后。。。。。。
- android利用jni获取自己apk的签名。
- ActionBar移动到界面底部
我建议你用广播,在Activity里面注册一个广播,然后在Service里面发送广播,这个是最常用的方法来把Service获取的数据 传给UI层。usb = new UsbDongleService();
usb.setHander(mHandler);
这样设置显然Service是无法收到mHandler对象的,UsbDongleService这个无需实例化,Android 系统会实例化的。
应该通过Service里面的Binder子类去设置一个对象。
我用的Messenger解决的,就是Binder子类吧?
因为这Service和Activity是在一个apk中的,我还觉得这算不算进程之间的通信。
跨进程 可以 有 Intent 广播 ,contentProvider 以及 AIDL