我正试图Service从我Activity的网页上寻找更改,这是一个私人应用,所以我不打扰电池寿命...
但是我想将数据从我的传递Service给我Activity......我似乎无法找到一种方法来调用Activity我的Service.我怎样才能做到这一点?
绑定服务是否更好FragmentActivity:
bindService(Intent, ServiceConnection, int);
Run Code Online (Sandbox Code Playgroud)
或者Fragment:
getActivity().bindService(Intent, ServiceConnection, int);
Run Code Online (Sandbox Code Playgroud)
什么是更好的做法?
这是我的代码Activity.启动一个Intent,然后一个Connection,对吧?
hello_service = new Intent(this, HelloService.class);
hello_service_conn = new HelloServiceConnection();
bindService( hello_service, hello_service_conn, Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)
但我的问题是......连接内部是什么?
class HelloServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName className,IBinder boundService ) {
}
public void onServiceDisconnected(ComponentName className) {
}
};
Run Code Online (Sandbox Code Playgroud)
有人能告诉我在ServiceConnected和onServiceDisconnected上添加了哪些代码?
我只想要一个基本的连接,以便我Activity和Service他人可以互相交谈.
编辑:我找到了一个很好的教程,我实际上可以关闭这个问题,除非有人想回答.http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/