有闲无聊
有闲无聊
  • 发布:2016-03-21 13:27
  • 更新:2019-08-09 18:01
  • 阅读:2300

调用Native.js,如何bindService?

分类:Native.js

我现在需要启动一个后台服务,且需要与Service通信,android里是通过bindService实现的,我如何在Native.js里实现bindService,我尝试写了一下,但Service并没有启动,按照http://ask.dcloud.net.cn/question/433 使用startService可以启动,不知道我这样写问题出在那里?

var main = plus.android.runtimeMainActivity();  

var Bundle = plus.android.importClass('android.os.Bundle');  
var bundle = new Bundle();  
bundle.putString("ip","121.42.185.114");  
bundle.putInt("port",9888);  
var Intent = plus.android.importClass('android.content.Intent');  
var intent = new Intent();  

intent.setClassName(main, "com.vox.socket.MinaSocketService");  
intent.putExtras(bundle);  

var mConnection = plus.android.implements("android.content.ServiceConnection",{  
    onServiceConnected:function(componentName,service){  
//      msService = service.getService();  
    }  
});  

main.bindService(intent,mConnection,plus.android.importClass('android.content.Context').plusGetAttribute("BIND_AUTO_CREATE"));
2016-03-21 13:27 负责人:无 分享
已邀请:
有闲无聊

有闲无聊 (作者)

已找到问题所在,最后一行改为:
main.bindService(intent,mConnection,plus.android.importClass('android.content.Context').BIND_AUTO_CREATE);
但是还是不明白plusGetAttribute和 . 的区别在哪里?文档描述的不是很清楚。

Android_磊子

Android_磊子

试试这样:
**var main = plus.android.runtimeMainActivity();
var Bundle = plus.android.importClass('android.os.Bundle');
var bundle = new Bundle();
bundle.putString("ip","121.42.185.114");
bundle.putInt("port",9888);
var Intent = plus.android.importClass('android.content.Intent');
var intent = new Intent();

intent.setClassName(main, "com.vox.socket.MinaSocketService");
intent.putExtras(bundle);

var mConnection = plus.android.implements("android.content.ServiceConnection",{
onServiceConnected:function(componentName,service){
// msService = service.getService();
},
onServiceDisconnected:function(componentName){
//do something
}
});
var Context = plus.android.importClass('android.content.Context');
main.bindService(intent,mConnection,.Context.BIND_AUTO_CREATE);**

1***@qq.com
mayflower

mayflower

请教一个问题:aidl是怎样导入的?(就是msService = service.getService(); 这里的msService )

该问题目前已经被锁定, 无法添加新回复