博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android学习总结(二)——Service基本概念和生命周期
阅读量:4584 次
发布时间:2019-06-09

本文共 3408 字,大约阅读时间需要 11 分钟。

      好了,前面我们已经学习了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文件中注册才能生效,代码如下:

 接着定义两个简单的启动服务器和停止服务器的按钮,在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!

 

 

 

转载于:https://www.cnblogs.com/nylcy/p/6501900.html

你可能感兴趣的文章
Climbing Stairs
查看>>
css遮罩层与fixed
查看>>
HTML5 Input 类型
查看>>
linux c语言 select函数用法 分类: arm-linux-...
查看>>
浏览网页出现右键查看源代码无效时
查看>>
动态生成的元素绑定KindEditor
查看>>
03--maven4myeclipse配置
查看>>
关于datatable的数据绑定问题
查看>>
c#函数中处理对象的问题
查看>>
转 top、postop、scrolltop、offsetTop、scrollHeight、offsetHeight、clientHeight
查看>>
2017-12-27练习
查看>>
NET设计规范(二) 命名规范
查看>>
VMware 9.0.1安装Mac OS X Mountain Lion 10.8.2
查看>>
SSL延迟
查看>>
android新手关于左右滑动的问题,布局把<android.support.v4.view.ViewPager/><ImageView/> 放在上面就不行了。...
查看>>
python第二十一天---昨天没写完作业
查看>>
爬虫基础--IO多路复用单线程异步非阻塞
查看>>
Johnny Solving CodeForces - 1103C (构造,图论)
查看>>
oracle数据库自学笔记(持续更新中……)
查看>>
BZOJ3685: 普通van Emde Boas树
查看>>