设为首页 加入收藏

TOP

Android四大组件之Service(二)
2019-09-03 03:44:49 】 浏览:106
Tags:Android 四大 组件 Service
)方法; 2.如果service已处于运行中,调用startService()不会执行onCreate()方法,只执行onStartCommand()方法。 也就是说,onCreate()只会在第一次创建service时候调用,多次执行startService()不会重复调用onCreate(),此方法适合完成一些初始化工作。

  • onStartCommand() 如果多次执行了Context的startService()方法,那么Service的onStartCommand()方法也会相应的多次调用。onStartCommand()方法很重要,我们在该方法中根据传入的Intent参数进行实际的操作,比如会在此处创建一个线程用于下载数据或播放音乐等。

  • onBind() Service中的onBind()方法是抽象方法,Service类本身就是抽象类,所以onBind()方法是必须重写的,即使我们用不到。

  • onDestroy() 在销毁的时候会执行Service的该方法。

  • 这几个方法都是回调方法,且在主线程中执行,由Android操作系统在合适的时机调用。

    注意:每个Service必须在manifest中 通过<service>来声明。

    <service android:name="com.demo.service.MyService" > 
      ... 
    </service>

    现在我们通过继承Service的方式定义了我们自己的MyService类,并且在manifest中声明了我们的MyService,接下来我们应该启动我们自己的服务。

    启动Service

    第一种方式:我们是通过一个Intent对象,并调用startService()方法来启动MyService

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

    注意:假如我们是通过点击Button执行上面的代码,那么第一次点击的时候回执行其中的onCreate()onStartCommand()方法,但是当我们第二次点击的时候就只会执行onStartCommand()方法。

    为什么会这样呢? 这是由于onCreate()方法只会在Service第一次被创建的时候调用,如果当前Service已经被创建过了(第一次点击创建了MyService),不管怎样调用startService()方法,onCreate()方法都不会再执行。

    第二种方式:通过bindService启动Service

    bindService启动服务特点: 1.bindService启动的服务和调用者之间是典型的client-server模式。调用者是clientservice则是server端。service只有一个,但绑定到service上面的client可以有一个或很多个。这里所提到的client指的是组件,比如某个Activity

    2.client可以通过IBinder接口获取Service实例,从而实现在client端直接调用Service中的方法以实现灵活交互,这在通过startService()方法启动中是无法实现的。

    3.bindService启动服务的生命周期与其绑定的client息息相关。当client销毁时,client会自动与Service解除绑定(client会有ServiceConnectionLeaked异常,但程序不会崩溃)。当然,client也可以明确调用ContextunbindService()方法与Service解除绑定。当没有任何clientService绑定时,Service会自行销毁。

    启动了之后,当我们想停止服务的时候该怎么做呢?

    停止Service

    第一种方式:我们也是通过一个Intent对象,并调用stopService()方法来停止MyService

    Intent stopIntent = new Intent(this, MyService.class);
    stopService(stopIntent); 

    第二种方式:调用unbindService(conn)方法来停止MyService

    unbindService(ServiceConnection conn)
    Service和Activity通信

    在上面我们高高兴兴的启动了Service了,但是细心的你可能发现了,貌似我们仅仅只是启动了而已,ActivityService并没有多少"交流",下面我们就让ActivityService交流一下。

    public class MyService extends Service {
    ?
        public static final String TAG = "MyService";
    ?
        private MyBinder mBinder = new MyBinder();
    ?
        @Override
        public void onCreate() {
            super.onCreate();
            Log.d(TAG, "onCreate() executed");
        }
    ?
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.d(TAG, "onStartCommand() executed");
            return super.onStartCommand(intent, flags, startId);
        }
    ?
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.d(TAG, "onDestroy() executed");
        }
    ?
        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }
    ?
        class MyBinder extends Binder {
    ?
            public void startDownload() {
                Log.d("TAG", "startDownload() executed");
                // 执行具体的下载任务
            }
    ?
        }
    ?
    }

    接下来我们在MainActivity中通过Button来绑定Service和解除绑定

    public class MainActivity extends Activity implements OnClickListener {
            
        private Button bindService;
    ?
        private Button unbindService;
    ?
        private MyService.MyBinder myBinder;
    ?
        private ServiceConnection connection = new ServiceConnection() {
    ?
            @Override
            public void on
    首页 上一页 1 2 3 下一页 尾页 2/3/3
    】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
    上一篇JEB 无源码调试 以dvm smali字节.. 下一篇细数 SharedPreferences 的那些槽..

    最新文章

    热门文章

    Hot 文章

    Python

    C 语言

    C++基础

    大数据基础

    linux编程基础

    C/C++面试题目