android多个服务器绑定,android – 将服务绑定到多个活动

我的服务被正确地绑定到我的第一个活动但是当我尝试将它绑定到第二个活动时它不起作用

这是我的第一个活动的onresume和暂停的代码

@Override

protected void onResume() {

super.onResume();

connection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

service = null;

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

shareInfos.this.service = (IService) service;

}

};

bindService(new Intent(this, shareInfos.class), connection,

Context.BIND_AUTO_CREATE);

}

@Override

protected void onPause() {

super.onPause();

if (service != null) {

service = null;

unbindService(connection);

}

}

我对第二个活动做了同样的事情,但是当我尝试使用该服务时,它总是为空

这是我的第二个活动的代码:

@Override

protected void onResume() {

super.onResume();

connection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

service = null;

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

shareInfos.this.service = (IService) service;

}

};

bindService(new Intent(this, shareInfos.class), connection,

Context.BIND_AUTO_CREATE);

}

@Override

protected void onPause() {

super.onPause();

if (service != null) {

service = null;

unbindService(connection);

}

}

多数民众赞成是我服务的代码:

public class ExampleService extends AbstractService {

private static final String SERVICE_NAME = "ExampleService";

public ExampleService() {

super(SERVICE_NAME);

}

@Override

public AbstractRegistration getRegistration() {

return new AbstractRegistration() {

@Override

public String getApplicationName() {

return getResources().getString(R.string.application_name);

}

@Override

public String getApplicationDescription() {

return getResources().getString(R.string.application_description);

}

@Override

public PendingIntent getApplicationSettings() {

return PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), ExampleActivity.class), 0);

}

@Override

public boolean requiresStorage() {

return true;

}

@Override

public boolean requiresQueries() {

return true;

}

@Override

public boolean requiresRecognition() {

return true;

}

};

}

}

这是我的清单文件:

package="eu.gambas.example.android" >

android:minSdkVersion="10"

android:targetSdkVersion="15" />

android:name="android.permission.WRITE_EXTERNAL_STORAGE"

android:maxSdkVersion="18" />

android:name="android.permission.READ_EXTERNAL_STORAGE"

android:maxSdkVersion="18" />

android:icon="@drawable/icon"

android:label="@string/application_name" >

android:name="com.example.egm.exampleandroid.ExampleActivity"

android:label="@string/application_name" >

android:name="com.example.egm.exampleandroid.ExampleService"

android:exported="true" >

在此先感谢您的帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值