如何开始(和绑定)远程服务机器人是在另一个应用程序(不同的包中)实现的? [英] how do i start (and bind) a remote service in android that is implemented in another app (different package)?
问题描述
我有点坚持与Android的远程服务。事情是我在包abc的实现的远程服务,我想其他应用程序能够访问该服务。我摆脱了整个蹩脚AIDL-的东西,设计通过广播意图工作,服务的接口。正常工作为止...
i'm a bit stuck with remote services in android. thing is i implemented a remote service in package "a.b.c" and i want other applications to be able to access this service. i got rid of the whole crappy aidl-stuff and designed the "interface" of the service to work via broadcasted intents. works fine so far...
问题是:我如何获得不同的
应用程序(不同的包装,不同的项目,甚至不同的开发商,...)来启动/停止该服务?
problem is: how do i get a different
application (different package, different project, maybe even a different developer, ...) to start/stop the service?
package d.e.f;
import a.b.c.*;
public class main extends Activity {
protected ImyService myService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(ImyService.class.getName());
bindService(intent, sConnection, Context.BIND_AUTO_CREATE);
}
protected ServiceConnection sConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
wlService = ImyService.Stub.asInterface(binder);
ServiceConnected = true;
Toast.makeText(main.this, "service connected", Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
wlService = null;
ServiceConnected = false;
Toast.makeText(main.this, "service disconnected", Toast.LENGTH_SHORT).show();
}
};
}
这将立即启动时崩溃我的应用程序。我做了什么错?我将如何得到这个工作?
this will crash my app immediately on startup. what did i do wrong? how will i get this to work?
一旦它的运行,命令和数据将通过广播传递。所以这应该是没有真正的问题...
once it's running, commands and data will be passed via broadcasts. so that should be no real problem...
推荐答案
第1步:建立一个<意向滤光器>
您<服务>
与<作用>
字符串
Step #1: Set up an <intent-filter>
for your <service>
with an <action>
string.
步骤#2:使用该操作字符串意图
您使用bindService()
(如新意图(this.is.my.custom.ACTION)
)
Step #2: Use that action string for the Intent
you use with bindService()
(e.g., new Intent("this.is.my.custom.ACTION")
)
这篇关于如何开始(和绑定)远程服务机器人是在另一个应用程序(不同的包中)实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!