【发布时间】:2010-08-03 12:06:41
【问题描述】:
我有一个服务组件(我的所有应用程序的共同任务),它可以被任何应用程序调用。我正在尝试从所有活动中访问服务对象,我注意到创建服务 [startService(intent)] 的那个具有正确的信息。但是休息并没有得到所需的信息。我的代码如下:
// 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;
}
如果我调用 startService(intent)。它会创建一个新服务并与其他服务并行运行。
如果我不调用 startService(intent),serviceObj.getData() 将返回 null 值。
谁能告诉我哪里出错了。
任何类型的指针都会非常有用..
感谢和问候, 维奈
【问题讨论】:
标签: android binding service android-activity