本文介绍了多个活动结合到服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(对我所有的应用程序的共同任务),它可以由任何应用程序调用的服务组件。我试图从所有活动访问服务对象,我注意到其中创建服务[startService(意向)的人有正确的咨询无论。但其余的没有得到所需要的咨询无论。我的code是如下:

  // Activity.java
公共无效的onCreate(包savedInstanceState){
    ...

    意向意图=新的意图(this.context,Service.class);
    this.context.startService(意向);
    this.context.bindService(意向,这一点,Context.BIND_AUTO_CREATE);
    ...
    字符串结果= serviceObj.getData();
}

公共无效onServiceConnected(组件名名称,服务的IBinder){
    serviceObj =((Service.LocalBinder)服务).getService();
    timer.scheduleAtFixedRate(任务,5000,60000);
}



// Service.java

私人最终的IBinder mBinder =新LocalBinder();

公共类LocalBinder扩展粘合剂{
    服务的getService(){
        返回Service.this;
    }
}

公共无效的onCreate(){
    super.onCreate();
    上下文= getApplicationContext();
}

公共无效ONSTART(意向意图,诠释startId){

......一些处理都是在这里完成...

}

公众的IBinder onBind(意向意图){
    返回mBinder;
}
 

如果我调用startService(意向)。它创建一个新的服务和并行运行的其他服务。

如果我不调用startService(意向),serviceObj.getData()retuns空值。

任何一个可以赐教我在哪里出了问题。

任何类型的指针将是非常有用的。

感谢和问候,维奈

解决方案

没有,没有。这里将是您的服务运行最多的一个实例。

startService()无关吧,从我读您的code。您正在尝试使用 serviceObj 的onCreate()。这行不通。 bindService()是一个异步调用。不能使用 serviveObj 直到 onServiceConnected()被调用。 onServiceConnected()的onCreate将不会被调用,直到某个时候()的回报。

另外:

  • 虽然有些时候,你可能需要两个 startService() bindService(),他们不是这两种情况下需要在正常的情况下。
  • 请不要使用 getApplicationContext()

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. 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). 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.

Can any one enlighten me where have I gone wrong.

Any kind of pointer will be very useful..

Thanks and regards,Vinay

解决方案

No, it does not. There will be at most one instance of your service running.

startService() has nothing to do with it, from my reading of your code. You are attempting to use serviceObj in onCreate(). That will never work. bindService() is an asynchronous call. You cannot use serviveObj until onServiceConnected() is called. onServiceConnected() will not be called until sometime after onCreate() returns.

Also:

  • While there are cases when you might need both startService() and bindService(), they are not both needed in the normal case.
  • Do not use getApplicationContext().

这篇关于多个活动结合到服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 02:39