Pau*_*aul 6 android android-service
我的理解是,如果我想要一个服务运行,即使没有任何限制,它必须首先使用startService(Intent i)启动.
我的问题是,如果我想在启动它后立即绑定到服务,下面的代码是否保证使用startService()创建服务?
服务类中的静态方法:
public static void actStart(Context ctx) {
Intent i = new Intent(ctx, BGService.class);
i.setAction(ACTION_START);
ctx.startService(i);
}
Run Code Online (Sandbox Code Playgroud)
绑定活动:
BGService.actionStart(getApplicationContext());
bindService(new Intent(this, BGService.class), serviceConnection, Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)
我不确定您想在这里做什么,但是“Context.BIND_AUTO_CREATE”会创建服务,然后绑定到该服务,即使它尚未启动。
现在如果你想在绑定后立即访问它,你可以使用serviceConnection的onServiceConnected()方法:
new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
//put your code here...
} ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1586 次 |
| 最近记录: |