相关疑难解决方法(0)

绑定到服务的多个活动

我有一个服务组件(我所有应用程序的常见任务),可以由任何应用程序调用.我试图从所有活动中访问服务对象,我注意到创建服务的那个[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 …
Run Code Online (Sandbox Code Playgroud)

service binding android android-activity

0
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

android-activity ×1

binding ×1

service ×1