gpt4 book ai didi

android - ServiceConnection.onServiceConnected() 和 startService()

转载 作者:搜寻专家 更新时间:2023-11-01 07:37:13 25 4
gpt4 key购买 nike

我有一个非常简单的 Activity :

public class MainActivity extends Activity
{
private Intent serviceIntent;
public MainService mainService;

public ServiceConnection sc = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName className, IBinder service)
{
mainService = ((MainService.MainServiceBinder)service).getService();
Log.v("xxx", "[MainActivity]: onServiceConnected()");
}

@Override
public void onServiceDisconnected(ComponentName arg0)
{
mainService = null;
Log.v("xxx", "[MainActivity]: onServiceDisconnected()");
}
};

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
serviceIntent = new Intent(this, MainService.class);
}

@Override
public void osStart()
{
super.onStart();
// To call onServiceConnected() if the service already started
bindService(serviceIntent, sc, BIND_DEBUG_UNBIND)
}

@Override
public void onStop()
{
unbindService(sc);
super.onStop();
}

// android:onClick procedure for Button in layout/main.xml
public void doStartStopService(View Sender)
{
if(null == mainService)
{
startService(serviceIntent);
}
else
{
stopService(serviceIntent);
}
}
}

和一个非常简单的服务:

public class MainService extends Service implements Runnable
{
private boolean isInterrupted = false;
private Thread thread;

class MainServiceBinder extends Binder
{
MainService getService()
{
return MainService.this;
}
}

@Override
public void onCreate()
{
Log.v("xxx", "[MainService]: onCreate()");
super.onCreate();
thread = new Thread(this);
thread.start();
}

// 1.6 only
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Log.v("xxx", "[MainService]: onStart()");
}

@Override
public void onDestroy()
{
Log.v("xxx", "[MainService]: onDestroy()");
if(thread.isAlive())
{
isInterrupted = true;
}
super.onDestroy();
}

@Override
public IBinder onBind(Intent arg0)
{
Log.v("xxx", "[MainService]: onBind()");
return new MainServiceBinder();
}

@Override
public boolean onUnbind(Intent intent)
{
Log.v("xxx", "[MainService]: onUnbind()");
return true;
}

@Override
public void run()
{
Log.v("xxx", "[MainService]: run() started");

while(!isInterrupted)
{
// ...
}

Log.v("xxx", "[MainService]: run() exiting");
}

}

当我在一个循环中调用 doStartStopService() 时,我得到了下一个日志:

[1]首先startService():

02-10 07:31:49.775: V/xxx(16306): [MainService]: onCreate()
02-10 07:31:49.975: V/xxx(16306): [MainService]: onBind()
02-10 07:31:50.005: V/xxx(16306): [MainService]: onStart()
02-10 07:31:50.165: V/xxx(16306): [MainActivity]: onServiceConnected()
02-10 07:31:50.175: V/xxx(16306): [MainService]: run() started

[2] 首先停止服务():

02-10 07:31:52.205: V/xxx(16306): [MainActivity]: onServiceDisconnected()
02-10 07:31:52.205: V/xxx(16306): [MainService]: onUnbind()
02-10 07:31:52.215: V/xxx(16306): [MainService]: onDestroy()
02-10 07:31:52.235: V/xxx(16306): [MainService]: run() exiting

[3] 第二个 startService() - 没有 onBind() 和 onServiceConnected() 调用:

02-10 07:31:54.355: V/xxx(16306): [MainService]: onCreate()
02-10 07:31:54.365: V/xxx(16306): [MainService]: onStart()
02-10 07:31:54.365: V/xxx(16306): [MainService]: run() started

为什么当我第二次调用 startService() 时,onBind() 和 onServiceConnected() 函数没有被调用?

最佳答案

如果你绑定(bind)到服务,服务onBind会调用,所以第二个startService调用,没有绑定(bind)到这个服务,你需要调用bindService( serviceIntent, sc, BIND_DEBUG_UNBIND) 再次。

关于android - ServiceConnection.onServiceConnected() 和 startService(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9222415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com