BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / mobile-terminal-at / #22782同步于 2015/6/9
该镜像源已超过 30 天没有更新,可能在源站已被删除。
MobileTerminalAT机器人发帖

[问题]大神帮解答一下service通信的问题

SeaH
2015/6/9镜像同步11 回复
在activity想通过bundle给它的service发送数据,那么service在哪个函数下进行接收? 调试了很久,用intent.putExtra() 和在service中intent.getStringExtra() 取到了数据。但是之前使用bundle却不行。onStartCommand()里面是这样写的: Bundle bundle = intent.getExtras(); String a = bundle.getString("xx"); //这里会报错,使程序闪退。 网上又有通过parcel来实现数据交互。然后我在activity写了下面这个方法。但是这个方法会致使程序闪退。不懂为何会闪退。 void mOnTransact(){ Parcel data = Parcel.obtain(); //Parcel is a container. Retrive a new parcel object from the pool. data.writeInt(3); //write data which is received into the parcel container. data.writeString("hhhhh activity"); //data need to be sent. Parcel reply = Parcel.obtain(); try{ mBinder.transact(IBinder.LAST_CALL_TRANSACTION, data, reply, 0); }catch(RemoteException e){ e.printStackTrace(); Log.e("RemoteException ========", e.getMessage()); } Log.i("Tag ====", "service return" + reply.readInt()); Log.i("Tag ====", "service return" + reply.readString()); } 在service里,定义了内部类: public class LocalBinder extends Binder{ /*In the activity, we can bind the service by invoking this method.*/ public WifiDataService getService(){ return WifiDataService.this; } /* * used to transact data with activity. */ @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException{ /*data: data to send to the taget. *reply: data to be received from the target.. */ Log.i("Tag ====", "--> " + reply.readInt()); Log.i("Tag ====", "-->" + reply.readString()); data.writeInt(2); data.writeString("hhhh onTransact in service"); return super.onTransact(code, data, reply, flags); } } 不知道怎么弄图片上去,只能这样粘源码了== 继续求大神。
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
txl39100机器人#1 · 2015/6/9
startService的时候带的Intent可以在Service的onStartCommand里面取到
SeaH机器人#2 · 2015/6/9
是这样写的,但是在onStartCommand取到的值为null。嗯,我在调试的时候,在intent里放了一个字符串常量。 【 在 txl39100 的大作中提到: 】 : startService的时候带的Intent可以在Service的onStartCommand里面取到
lixing机器人#3 · 2015/6/9
亲测取不到,还是通过回调传数据吧。 【 在 txl39100 的大作中提到: 】 : startService的时候带的Intent可以在Service的onStartCommand里面取到 : 发自「贵邮」
txl39100机器人#4 · 2015/6/9
自行参考官方文档~~ intent The Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY. 【 在 lixing 的大作中提到: 】 : 亲测取不到,还是通过回调传数据吧。 : : 发自「贵邮」
zishi机器人#5 · 2015/6/9
BindService官网有例程的吧?
qq125537机器人#6 · 2015/6/9
1. 在onStartCommand() 可以使用bundle = intent.getExtras() ,闪退的code或者error message是什么? 建议仔细检查一遍code,service 是否start/bind 正确? 2. Log.i("Tag ====", "--> " + reply.readInt()); Log.i("Tag ====", "-->" + reply.readString()); data.writeInt(2); data.writeString("hhhh onTransact in service"); 这一段 data跟reply弄反了
SeaH机器人#7 · 2015/6/10
[ema11]多谢~ 我试着添加try-catch和一些log打印,但是实际上,无论是try还是catch里的log都没有打印。当我注释掉intent传入的数据的时候,程序才可以正常运行。 是因为,我在启动service的时候,使用的是startService(intent)?我看资料说,startSewrvice()不会和client有数据交互。有数据交互,要使用bindService()? 【 在 qq125537 的大作中提到: 】 : 1. 在onStartCommand() 可以使用bundle = intent.getExtras() ,闪退的code或者error message是什么? 建议仔细检查一遍code,service 是否start/bind 正确? : 2. : Log.i("Tag ====", "--> " + reply.readInt()); : ...................
qq125537机器人#8 · 2015/6/10
你要是从service(server)回传Activity(client),就需要使用bindService了。 如果是startService,Activity可以通过intent传给service数据。 建议找找service视频、blog、developer等,看看能run通的代码是怎么写的。 【 在 SeaH 的大作中提到: 】 : 多谢~ 我试着添加try-catch和一些log打印,但是实际上,无论是try还是catch里的log都没有打印。当我注释掉intent传入的数据的时候,程序才可以正常运行。 : 是因为,我在启动service的时候,使用的是startService(intent)?我看资料说,startSewrvice()不会和client有数据交互。有数据交互,要使用bindService()?
yxyyinxinyu机器人#9 · 2015/6/10
aidl解任何service通信问题。。。 一个IServiceInterface 一个IServiceCallback 两边就可以随便通随便调用了