多个activity操作一个service

本文介绍了一个具体的Android应用程序中使用BindService机制的例子。该应用通过两个Activity与一个Service交互,展示了如何在不同组件间共享数据和服务状态。具体分析了Service生命周期方法的调用时机以及客户端如何通过Binder接口获取Service内部的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

service代码


public class UploadService extends Service{


private Context mContext;


private ServerDataManager mServerDataManager;

private int testId = 0;

@Override
public void onCreate() {
super.onCreate();
mContext = this;
testId = testId+10;
Log.e("aaaa", "======= UploadService onCreate");
}

@Override
public void onStart(Intent intent, int startId) {
Log.e("aaaa", "======= UploadService onStart");
super.onStart(intent, startId);
}

@Override
public void onRebind(Intent intent) {
Log.e("aaaa", "======= UploadService onRebind");
super.onRebind(intent);
}


@Override
public boolean onUnbind(Intent intent) {
Log.e("aaaa", "======= UploadService onUnbind");
return super.onUnbind(intent);
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("aaaa", "======= UploadService onStartCommand");
Log.e("aaaa", "======= UploadService testId : "+testId);
return super.onStartCommand(intent, flags, startId);
}

@Override
public IBinder onBind(Intent intent) {
Log.e("aaaa", "======= UploadService onBind");
Log.e("aaaa", "======= UploadService testId : "+testId);
return new UploadBind();
}


public class UploadBind extends Binder{

public UploadService getService(){
return UploadService.this;
}

public int getTestId(){
return testId;
}


public int updateTestId(int data){
testId = testId + data;
return testId;
}
}

@Override
public void onDestroy() {
super.onDestroy();
}


}


activity a 部分代码:


@Override
protected void onCreate() {

TestConn  conn = new TestConn();
Intent service = new Intent();
service.setClass(mContext, UploadService.class);
bindService(service, conn, BIND_AUTO_CREATE);

}


@Override
protected void onResume() {
Log.e("aaaaa", "======= MainActivity onResume : " + bind);
if(bind!=null){
Log.e("aaaaa", "======= MainActivity bind : " + bind.getTestId());
}
super.onResume();
}

private UploadBind bind;

class TestConn implements ServiceConnection{


@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
bind = (UploadBind)arg1;
}


@Override
public void onServiceDisconnected(ComponentName arg0) {


}

}



activity b 部分代码:


@Override
protected void onCreate() {
TestConn  conn = new TestConn();
Intent service = new Intent();
service.setClass(mContext, UploadService.class);
bindService(service, conn, BIND_AUTO_CREATE);

}


@Override
protected void onResume() {
Log.e("aaaaa", "======= MessageActivity onResume : " + bind);
if(bind!=null){
Log.e("aaaaa", "======= MessageActivity bind : " + bind.getTestId());
}
super.onResume();
}

private UploadBind bind;

class TestConn implements ServiceConnection{


@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
bind = (UploadBind)arg1;
}


@Override
public void onServiceDisconnected(ComponentName arg0) {


}

}



@Override
public void onBackClick() {
if(bind!=null){
Log.e("aaaaa", "======= MessageActivity onBackClick00 bind : " + bind.getTestId());
int data = bind.updateTestId(100);
Log.e("aaaaa", "======= MessageActivity onBackClick11 bind : " + data);
Log.e("aaaaa", "======= MessageActivity onBackClick22 bind : " + bind.getTestId());
}
finish();

}


流程:

activity a 以bindservice方式 启动 service,会执行service里面的onCreate, onBind方法,

跳转到activity b后,再次以bindservice方式 启动 service,不执行service里面的任何函数,

但是获得service里面的bind,改变service里面的某个变量x,

返回activity a,a获取service里面的bind,然后获取变量x,x的值为activity b设置的值



activity a 部分代码:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值