简体   繁体   English

绑定到服务的多个活动

[英]Multiple activities binding to a service

I have a service component (common task for all my apps), which can be invoked by any of the apps. 我有一个服务组件(我所有应用程序的共同任务),可以由任何应用程序调用。 I am trying to access the service object from the all activities, I noticed that the one which created the service [startService(intent)] has the right informaion. 我正在尝试从所有活动访问服务对象,我注意到创建服务[startService(intent)]的对象具有正确的信息。 But rest does not get the informaion needed. 但是休息并不能获得所需的信息。 My Code is as below: 我的代码如下:

// Activity.java
public void onCreate(Bundle savedInstanceState) {
    ...

    Intent intent = new Intent (this.context, Service.class) ;
    this.context.startService(intent) ;
    this.context.bindService(intent, this, Context.BIND_AUTO_CREATE) ;
    ...
    String result = serviceObj.getData() ;
}

public void onServiceConnected(ComponentName name, IBinder service) {
    serviceObj = ((Service.LocalBinder)service).getService();
    timer.scheduleAtFixedRate(task, 5000, 60000 ) ;
}



// Service.java

private final IBinder mBinder = new LocalBinder();

public class LocalBinder extends Binder {
    Service getService() {
        return Service.this;
    }
}

public void onCreate() {
    super.onCreate();
    context = getApplicationContext() ;
}

public void onStart( Intent intent, int startId ) {

... some processing is done here...

}

public IBinder onBind(Intent intent) {
    return mBinder;
}

If I invoke startService(intent). 如果我调用startService(intent)。 it creates a new service and runs in parallel to the other service. 它创建一个新服务并与其他服务并行运行。

If I don't invoke startService(intent), serviceObj.getData() retuns null value. 如果我不调用startService(intent),则serviceObj.getData()重新调整null值。

Can any one enlighten me where have I gone wrong. 任何人都可以启发我哪里做错了。

Any kind of pointer will be very useful.. 任何一种指针都将非常有用。

Thanks and regards, Vinay 感谢和问候,Vinay

If I invoke startService(intent). 如果我调用startService(intent)。 it creates a new service and runs in parallel to the other service. 它创建一个新服务并与其他服务并行运行。

No, it does not. 不,不是的。 There will be at most one instance of your service running. 您的服务最多将运行一个实例。

If I don't invoke startService(intent), serviceObj.getData() retuns null value. 如果我不调用startService(intent),则serviceObj.getData()重新调整null值。

startService() has nothing to do with it, from my reading of your code. 根据我对代码的阅读, startService()与它无关。 You are attempting to use serviceObj in onCreate() . 您正在尝试在onCreate()使用serviceObj That will never work. 那永远都行不通。 bindService() is an asynchronous call. bindService()是一个异步调用。 You cannot use serviveObj until onServiceConnected() is called. 在调用onServiceConnected()之前,不能使用serviveObj onServiceConnected() will not be called until sometime after onCreate() returns. 直到onCreate()返回后的某个时间才会调用onServiceConnected()

Also: 也:

  • While there are cases when you might need both startService() and bindService() , they are not both needed in the normal case. 在某些情况下,您可能同时需要startService()bindService() ,但在通常情况下并不需要。
  • Do not use getApplicationContext() . 不要使用getApplicationContext()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM