zoukankan      html  css  js  c++  java
  • Android学习总结(二)——Service基本概念和生命周期

          好了,前面我们已经学习了Activity的知识,相信大家也有一定的理解,但是还是不能放松,Android四大组件,我们才学习了一个而已,接下我们继续学习Service。计划总结如下内容:

      

    一.Service的基本概念

      service是Android中实现程序后台运行的解决方案,它非常适合执行那些不需要和用户交互而且还要求长期运行的任务。服务的运行不依赖于任何用户界面,即使当程序被切换到后台,或者用户打开了另一个应用程序,服务仍然能够保持正常运行。服务并不是运行在一个独立的进程当中,而是依赖创建服务时所在的应用程序进程。当某一个应用程序进程被杀掉时,所有依赖于该进程的服务也会停止运行。但是我们千万不要被服务端的后台概念所迷住咯,实际上服务并不会自动开启线程,所有的代码都是默认运行在主线程当中的,也就是说,我们需要在服务的内部手动创建子线程,并在这里执行具体的任务,否则就有出现主线程被阻塞住的情况。

    二.Service生命周期

         我们之前学习过Activity的生命周期,当然Service也有Service的生命周期,前面我们使用了onCreate()、onStart()、onResume()、onPause()、onStop()、onDestroy()和onRestart()方法都是在服务的生命周期内可能回调的方法。

    2.1 Service生命周期的图片

          图片的来源(http://www.runoob.com/w3cnote/android-tutorial-service-1.html)

      

     三、体验Service生命周期

     哈哈哈~说了那么多,我们还是用代码来验证service生命周期吧,代码才是最好的老师,首先自定义一个ServiceTest,重写相关的方法,用户在用Log打印验证,代码如下:

    package com.nyl.servicesummary;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    
    public class ServiceTest extends Service {
    
        private static final String TAG = ServiceTest.class.getSimpleName();
    
       //onBind()这个方法是service中唯一的一个抽象方法,所以必须在子类里实现
        @Override
        public IBinder onBind(Intent intent) {
            Log.i(TAG,"onBind方法被调用!");
            return null;
        }
    
        //Service被创建时调用
        @Override
        public void onCreate() {
            Log.i(TAG,"onCreate方法被调用");
            super.onCreate();
        }
    
        //Service被启动时调用
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i(TAG,"onStartCommand方法被调用");
            return super.onStartCommand(intent, flags, startId);
        }
    
        //服务销毁的时候调用
        @Override
        public void onDestroy() {
            Log.i(TAG,"onDestroy方法被调用");
            super.onDestroy();
        }
    }

    需要注意,每一个服务都需要在AndroidManifest.xml文件中注册才能生效,代码如下:

     <!--配置Service组件,同时配置一个action-->
            <service android:name=".ServiceTest">
                <intent-filter>
                    <action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/>
                </intent-filter>
            </service>

     接着定义两个简单的启动服务器和停止服务器的按钮,在MainActivity的按钮点击事件中分别调用startService()和stopService()这两个方法,代码如下:

    package com.nyl.servicesummary;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    /**
     * 开启服务和停止服务
     */
    public class MainActivity extends Activity implements View.OnClickListener {
    
        private Button btnStartService;
        private Button btnStopService;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //初始化布局控件
            btnStartService = (Button) findViewById(R.id.btnStartService);
            btnStopService = (Button) findViewById(R.id.btnStopService);
    
            btnStartService.setOnClickListener(this);
            btnStopService.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.btnStartService){
                //创建一个Intent对象,然后调用startService()方法来启动ServiceTest这个服务
                Intent intent = new Intent(this,ServiceTest.class);
                //开启服务
                startService(intent);
                return;
            }
            if (view.getId() == R.id.btnStopService){
                //创建一个Intent对象,然后调用stopService()方法来停止ServiceTest这个服务
                Intent intent = new Intent(this,ServiceTest.class);
                //停止服务
                stopService(intent);
                return;
            }
        }
    }

    最后的运行截图

    点击开启服务,如下图:

    点击停止服务,如下图:

    结果分析:

            从上面的运行结果我们可以验证我们生命周期图中解释的内容: 我们发现onBind()方法并没有被调用,另外多次点击启动Service,只会重复地调用onCreate和onStartCommand 方法!无论我们启动多少次Service,一个stopService就会停止Service!

     

  • 相关阅读:
    centos 安装 TortoiseSVN svn 客户端
    linux 定时任务 日志记录
    centos6.5 安装PHP7.0支持nginx
    linux root 用户 定时任务添加
    composer 一些使用说明
    laravel cookie写入
    laravel composer 安装指定版本以及基本的配置
    mysql 删除重复记录语句
    linux php redis 扩展安装
    linux php 安装 memcache 扩展
  • 原文地址:https://www.cnblogs.com/nylcy/p/6501900.html
Copyright © 2011-2022 走看看