android 绑定服务启动,关于android:何时启动并绑定服务被销毁?

当我发现两个矛盾的地方时,我正在浏览android中的服务文档:

在服务文档中,它是在管理服务的生命周期中指定的

These two paths are not entirely separate. That is, you can bind to a

service that was already started with startService(). For example, a

background music service could be started by calling startService()

with an Intent that identifies the music to play. Later, possibly when

the user wants to exercise some control over the player or get

information about the current song, an activity can bind to the

service by calling bindService(). In cases like this, stopService() or

stopSelf() does not actually stop the service until all clients

unbind.

但是在管理绑定服务的生命周期中有关绑定服务的文档中

However, if you choose to implement the onStartCommand() callback

method, then you must explicitly stop the service, because the service

is now considered to be started. In this case, the service runs until

the service stops itself with stopSelf() or another component calls

stopService(), regardless of whether it is bound to any clients.

可能是我,但我认为这些陈述是矛盾的。任何人都可以澄清一下...

同意文档可能更清晰。他们想说的是:

如果您调用startService(),则除非您直到调用stopSerivce()(或从服务内部的stopSelf()),否则该服务将继续运行。

如果您调用bindService(),则该服务将一直运行,除非并且直到您调用unbindService()

因此,如果您同时调用startService()和bindService(),则该服务将一直运行,直到您同时调用stopService和unbindService()。两者都不会单独停止服务。

创建了一个非常简单的"活动和服务",并运行了以下启动/停止/绑定/解除绑定序列。我观察到这些调用产生了以下结果。

绑定,解除绑定

bindService() caused:

onCreate()

onBind()

unbindService() caused:

onUnbind()

onDestroy()

启动绑定,解除绑定停

startService() caused:

onCreate()

onStartCommand()

bindService() caused:

onBind()

unbindService() caused:

onUnbind()

stopService() caused:

onDestroy()

启动绑定一站式解除绑定

startService() caused:

onCreate()

onStartCommand()

bindService() caused:

onBind()

stopService() caused:

-- nothing

unbindService() caused:

onUnbind()

onDestroy()

结合起停,解除绑定

bindService() caused:

onCreate()

onBind()

startService() caused:

onStartCommand()

stopService() caused:

-- nothing -- still running

unbindService() caused:

onUnbind()

onDestroy()

BIND的启动解除绑定停

bindService() caused:

onCreate()

onBind()

startService() caused:

onStartCommand()

unbindService() caused:

onUnbind()

stopService() caused:

onDestroy()

如您所见,在每种情况下,都同时调用了bind和start时,服务一直运行,直到取消绑定和stop都被调用。解除绑定/停止的顺序并不重要。

这是在我的简单测试应用程序中从单独的按钮调用的示例代码:

public void onBindBtnClick(View view) {

Intent intent = new Intent(MainActivity.this, ExampleService.class);

bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

}

public void onUnbindBtnClick(View view) {

if (serviceIsBound) {

unbindService(serviceConnection);

serviceIsBound = false;

}

}

public void onStartBtnClick(View view) {

Intent intent = new Intent(MainActivity.this, ExampleService.class);

startService(intent);

}

public void onStopBtnClick(View view) {

Intent intent = new Intent(MainActivity.this, ExampleService.class);

exampleService.stopService(intent);

}

感谢您对此进行的研究

很棒的解释...很有帮助..谢谢

迄今为止最清晰的解释。这应该在文档中可用。做得好!

被doc弄糊涂了:"当客户端与服务完成交互后,它将调用unbindService()解除绑定。当没有客户端绑定到服务时,系统将销毁该服务。"该答案清楚地表明,在调用unbindService之后,即使是单边界服务(带有Context.BIND_AUTO_CREATE)也将仍然有效。它只会在调用selfStop之后销毁。这与bindService然后startService流相同。谢谢!

实际上,两个段落都是相辅相成的(尽管它们的措词可能会误导他人),并且两个段落都与文档中的图像一致。我们来看一下:

These two paths are not entirely separate. That is, you can bind to a service that was already started with startService(). For example, a background music service could be started by calling startService() with an Intent that identifies the music to play. Later, possibly when the user wants to exercise some control over the player or get information about the current song, an activity can bind to the service by calling bindService(). In cases like this, stopService() or stopSelf() does not actually stop the service until all clients unbind.

精髓是:如果启动服务,然后将客户端绑定到该服务,然后尝试停止该服务,则在所有客户端解除绑定之前,该服务不会停止(销毁)。第二段并不矛盾,它完善了这一说法。

However, if you choose to implement the onStartCommand() callback method, then you must explicitly stop the service, because the service is now considered to be started. In this case, the service runs until the service stops itself with stopSelf() or another component calls stopService(), regardless of whether it is bound to any clients.

这意味着:即使没有客户端绑定到已启动且绑定的服务,直到明确停止该服务为止。当然,在此措词可能会更清晰一些。但是,文档中给出的生命周期图显示了这一点(而且我很确定我已经在"现实生活"中观察到了这一点,尽管我目前尚无直接的例子):

3bd3091a9b085a4956057e00a91a1513.png

因此,假设我想取消绑定并停止已启动和绑定的服务,我应该在取消绑定之后调用stopService吗?还是没关系?

顺序无关紧要。在并非所有客户端都解除绑定并且已停止之前,该服务将不会被销毁(如您在@ChuckKrutsinger的回复中所看到的)。

根据此图在混合服务中,在所有客户端都调用了Unbind onDestroy()之后,是否正确?onUnbind()和onDestroy()之间是否应存在直接链接?

是的,它有效。

我想完成一个示例代码:

我必须使用一个由活动启动的服务来制作一个应用程序,该活动必须调用该服务中的某些方法,即使该活动被杀死,该服务也必须在后台运行,并且当该活动重新启动时,它不必重新启动服务(如果正在运行)。希望对您有所帮助,您可以了解它如何与日志一起使用。

这就是代码:

public class MyActivity extends Activity{

private MyService myService;

private boolean mIsBound = false;

private ServiceConnection mConnection = new ServiceConnection() {

public void onServiceConnected(ComponentName className, IBinder binder) {

MyService.MyBinder b = (MyService.MyBinder) binder;

myService = b.getService();

mIsBound = true

//Do something

// Here you can call : myService.aFonctionInMyService();

}

public void onServiceDisconnected(ComponentName className) {

// Do something

mIsBound = false;

}

}

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

//Checked if my service is running

if (!isMyServiceRunning()) {

//if not, I start it.

startService(new Intent(this,MyService.class));

}

}

private boolean isMyServiceRunning() {

ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

for (RunningServiceInfo service : manager

.getRunningServices(Integer.MAX_VALUE)) {

if (MyService.class.getName().equals(

service.service.getClassName())) {

return true;

}

}

return false;

}

@Override

protected void onResume() {

// TODO Auto-generated method stub

super.onResume();

doBindService();

}

//Connection to the Service

private void doBindService() {

bindService(new Intent(this,MyService.class), mConnection,

Context.BIND_AUTO_CREATE);

}

// Disconnection from the service

private void doUnbindService() {

if (mIsBound) {

// Detach our existing connection.

unbindService(mConnection);

}

}

@Override

protected void onPause() {

// TODO Auto-generated method stub

doUnbindService();

super.onPause();

}

}

public class MyService extends Service{

public static String Tag ="MyService";

private final IBinder mBinder = new MyBinder();

@Override

public void onCreate() {

// TODO Auto-generated method stub

super.onCreate();

Log.d(Tag,"onCreate()");

}

public class MyBinder extends Binder {

public LocationService getService() {

return LocationService.this;

}

}

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

Log.d(Tag,"onBind()");

return mBinder;

}

@Override

public boolean onUnbind(Intent intent) {

// TODO Auto-generated method stub

Log.d(Tag,"onUnBind()");

return super.onUnbind(intent);

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

// TODO Auto-generated method stub

Log.d(Tag,"onStartCommand()");

return START_STICKY;

}

@Override

public void onDestroy() {

// TODO Auto-generated method stub

Log.d(Tag,"onDestroy");

super.onDestroy();

}

public void aFonctionInMyService(){

//Do Something

}

}

isMyServiceRunning速度很慢,将其放在onCreate()活动中是不好的做法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值