Android Service系列(八)实操一下Service

    xiaoxiao2022-07-03  119

    service代码如下,清单文件已经申明

    public class TestService extends Service { private static final String TAG = "TestService"; @Nullable @Override public IBinder onBind(Intent intent) { Log.i(TAG,"onBind"); return new Binder(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG,"onStartCommand"); return super.onStartCommand(intent, flags, startId); } }

    下面不停调用startService

    发现:每调用一次  startService   Service的onStartCommand 就执行一次

     

    不停调用bindService

    bindService代码如下

    bindService(new Intent(this, TestService.class), new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i(TAG,"onServiceConnected"); } @Override public void onServiceDisconnected(ComponentName name) { Log.i(TAG,"onServiceDisconnected"); } },BIND_AUTO_CREATE);

    发现,第一次是onBind调用,后面每次调用bindService,onServiceConnected就会回调一次,但是Serviced onBind方法则不再调用!

    看来和startService还有些区别

    最新回复(0)