如何通过意图访问“活动”从服务类? [英] How to access 'Activity' from a Service class via Intent?
问题描述
我是新来的Android编程 - 所以我没有语境和意图十分清楚的认识。
我想知道有没有办法从服务类访问的活动?
即比方说,我有2个班 - 从活动一延伸,从服务等延伸,我已经创建了一个意向在我的Activity类来启动服务
或者说,如何从我的活动级访问服务类的实例 - 因为在这样的工作流程服务类不能直接实例化后我以活动code
公共类MainActivity延伸活动{
。
。
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
startService(新意图(这一点,CommunicationService.class));
。
。
}
公共类CommunicationService扩展服务实现..... {
。
。
@覆盖
公众诠释onStartCommand(最终意图的意图,诠释标志,最终诠释startId){
super.onStartCommand(意向,标志,startId);
....
}
}
您可以使用bindService(意向意图,ServiceConnection康涅狄格州,诠释标志),而不是startService启动服务。而连接
将是一个内部类就像:
私人ServiceConnection康恩=新ServiceConnection(){
@覆盖
公共无效onServiceConnected(组件名名称,服务的IBinder){
mMyService =((CommunicationService.MyBinder)服务).getService();
}
@覆盖
公共无效onServiceDisconnected(组件名名){
}
};
mMyService
是CommunicationService的实例。
在你CommunicationService,只是重写:
公开的IBinder onBind(意向意图){
返回新MyBinder();
}
和你CommunicationService下面的类:
公共类MyBinder扩展粘合剂{
公共CommunicationService的getService(){
返回CommunicationService.this;
}
}
所以,你可以使用 mMyService
来访问您的活动的任何公共方法和字段。
另外,你可以在你的服务中使用回调接口来访问活动。
先写一个类似的界面:
公共接口OnChangeListener {
公共无效调用onChanged(INT进度);
}
和你的服务,请添加一个public方法:
公共无效setOnChangeListener(OnChangeListener onChangeListener){
this.mOnChangeListener = onChangeListener;
}
您可以使用给onChanged在服务中的任何地方,并在活动中实现刚:
公共无效onServiceConnected(组件名名称,服务的IBinder){
mMyService =((CommunicationService.MyBinder)服务).getService();
mMyService.setOnChangeListener(新OnChangeListener(){
@覆盖
公共无效调用onChanged(INT进度){
//任何你想做的事,例如更新进度
// mProgressBar.setProgress(进度);
}
});
}
PS:bindService将是这样的:
this.bindService(意向,康涅狄格州,Context.BIND_AUTO_CREATE);
不要忘记
保护无效的onDestroy(){
this.unbindService(康涅狄格州);
super.onDestroy();
}
希望它帮助。
I am new to Android programming - so I do not have very clear understanding of the 'Context' and the 'Intent'.
I want to know is there a way to access Activity from a Service class?
i.e. Let's say I have 2 classes - one extends from "Activity" and other extends from "Service" and I have created an intent in my Activity class to initiate the service.
Or, how to access the 'Service' class instance from my 'Activity' class - because in such workflow Service class is not directly instantiated by my Activity-code.
public class MainActivity extends Activity {
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, CommunicationService.class));
.
.
}
public class CommunicationService extends Service implements ..... {
.
.
@Override
public int onStartCommand(final Intent intent, int flags, final int startId) {
super.onStartCommand(intent, flags, startId);
....
}
}
You can use bindService(Intent intent, ServiceConnection conn, int flags) instead of startService to initiate the service. And the conn
will be a inner class just like:
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mMyService = ((CommunicationService.MyBinder) service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
mMyService
is the instance of your CommunicationService.
In your CommunicationService, just override:
public IBinder onBind(Intent intent) {
return new MyBinder();
}
and the following class in your CommunicationService:
public class MyBinder extends Binder {
public CommunicationService getService() {
return CommunicationService.this;
}
}
So you can use mMyService
to access any public methods and fields in your activity.
In addition, you can use callback interface to access activity in your service.
First write a interface like:
public interface OnChangeListener {
public void onChanged(int progress);
}
and in your service, please add a public method:
public void setOnChangeListener(OnChangeListener onChangeListener) {
this.mOnChangeListener = onChangeListener;
}
you can use the onChanged in your service anywhere, and the implement just in your activity:
public void onServiceConnected(ComponentName name, IBinder service) {
mMyService = ((CommunicationService.MyBinder) service).getService();
mMyService.setOnChangeListener(new OnChangeListener() {
@Override
public void onChanged(int progress) {
// anything you want to do, for example update the progressBar
// mProgressBar.setProgress(progress);
}
});
}
ps: bindService will be like this:
this.bindService(intent, conn, Context.BIND_AUTO_CREATE);
and do not forget
protected void onDestroy() {
this.unbindService(conn);
super.onDestroy();
}
Hope it helps.
这篇关于如何通过意图访问“活动”从服务类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!