基于Android Service 生命周期的详细介绍

   2015-08-26 0
核心提示:本篇文章小编为大家介绍,基于Android Service 生命周期的详解。需要的朋友参考下

Service概念及用途:

Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行,那我们什么时候会用到Service呢?比如我们播放音乐的时候,有可能想边听音乐边干些其他事情,当我们退出播放音乐的应用,如果不用Service,我们就听不到歌了,所以这时候就得用到Service了,又比如当我们一个应用的数据是通过网络获取的,不同时间(一段时间)的数据是不同的这时候我们可以用Service在后台定时更新,而不用每打开应用的时候在去获取。

Service生命周期 :

Android Service的生命周期并不像Activity那么复杂,它只继承了onCreate(),on Start(),onDestroy()三个方法,当我们第一次启动Service时,先后调用了onCreate(),on Start()这两个方法,当停止Service时,则执行onDestroy()方法,这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行on Start()方法。

Service与Activity通信:

Service后端的数据最终还是要呈现在前端Activity之上的,因为启动Service时,系统会重新开启一个新的进程,这就涉及到不同进程间通信的问题了(AIDL),当我们想获取启动的Service实例时,我们可以用到bindServiceunBindService方法,它们分别执行了Service中IBinder()和onUnbind()方法。

1、添加一个类,在MainActivity所在包之下

复制代码 代码如下:

public class LService extends Service {
 private static final String TAG = "LService";
 @Override
 public IBinder onBind(Intent intent) {
  Log.i(TAG, "onbind");
  return null;
 }
 @Override
 public void onCreate() {
  Log.i(TAG, "oncreate");
  super.onCreate();
 }
 @Override
 public void on Start(Intent intent, int startId) {
  Log.i(TAG, "on start");
  super.on Start(intent, startId);
 }
 @Override
 public void onDestroy() {
  Log.i(TAG, "ondestoty");
  super.onDestroy();
 }
 @Override
 public boolean onUnbind(Intent intent) {
  Log.i(TAG, "onubind");
  return super.onUnbind(intent);
 }
 public String getSystemTime() {
  Time t = new Time();
  t.setToNow();
  return t.toString();
 }
 public class LBinder extends Binder {
  LService getService() {
   return LService.this;
  }
 }
}



 2、在程序界面文件中添加控件
复制代码 代码如下:

<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="wecclome to Livingstone's bolg" />

<Button
android:id="@+id/startservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="startService" />

<Button
android:id="@+id/stopservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stopService" />

<Button
android:id="@+id/bindservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="bindService" />

<Button
android:id="@+id/unbindservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="unbindService" />


3、修改MainActivity中的方法,以及让MainActivity类实现OnClickListener接口
复制代码 代码如下:

public class MainActivity extends Activity implements OnClickListener {
 private LService mLService;
 private TextView mTextView;
 private Button startServiceButton;
 private Button stopServiceButton;
 private Button bindServiceButton;
 private Button unbindServiceButton;
 private Context mContext;
 // 这里需要用到ServiceConnection,在Context.bindService和context.unBindService()里用到
 private ServiceConnection mServiceConnection = new ServiceConnection() {
  // 当bindService时,让TextView显示LService里getSystemTime()方法的返回值
  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   mLService = ((LService.LBinder) service).getService();
   mTextView.setText("I am from Service :" + mLService.getSystemTime());
  }
  public void onServiceDisconnected(ComponentName name) {
  }
 };
 public void setupViews() {
  mContext = MainActivity.this;
  mTextView = (TextView) findViewById(R.id.text);

  startServiceButton = (Button) findViewById(R.id.startservice);
  stopServiceButton = (Button) findViewById(R.id.stopservice);
  bindServiceButton = (Button) findViewById(R.id.bindservice);
  unbindServiceButton = (Button) findViewById(R.id.unbindservice);

  startServiceButton.setOnClickListener(this);
  stopServiceButton.setOnClickListener(this);
  bindServiceButton.setOnClickListener(this);
  unbindServiceButton.setOnClickListener(this);
 }
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  setupViews();
 }
 @Override
 public void onClick(View v) {
  if (v == startServiceButton) {
   Intent i = new Intent(MainActivity.this, LService.class);
   mContext.startService(i);
  } else if (v == stopServiceButton) {
   Intent i = new Intent(MainActivity.this, LService.class);
   mContext.stopService(i);
  } else if (v == bindServiceButton) {
   Intent i = new Intent(MainActivity.this, LService.class);
   mContext.bindService(i, mServiceConnection, BIND_AUTO_CREATE);
  } else {
   mContext.unbindService(mServiceConnection);
  }
 }
}


4、注册Service

<service
  android:name=".LService"
  android:exported="true" >
</service>

5、运行程序

基于Android Service 生命周期的详细介绍程序界面

点击startService基于Android Service 生命周期的详细介绍此时调用程序设置里面可以看到Running Service有一个LService

点击stopService基于Android Service 生命周期的详细介绍

点击bindService基于Android Service 生命周期的详细介绍此时Service已经被关闭

点击unbindService基于Android Service 生命周期的详细介绍

先点击startService,再依次点击bindService和unbindService

基于Android Service 生命周期的详细介绍

 
标签: android 生命周期
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 说一说Android Studio和IDEA中一个很有用的内存调试插件
    说一说Android Studio和IDEA中一个很有用的内存
    JetBrains JVM Debugger Memory View plugin 在我最近的研发活动期间寻找新的工具,以提高我的开发经验,使Android Studio的生活更轻松,我发现一个有用的插件,我从来没有听说过。 这就是为什么,我决定写这个强大的工具,它如何帮助我与内存调试我的应用程
  • 安卓中通知功能的具体实现
    安卓中通知功能的具体实现
    通知[Notification]是Android中比较有特色的功能,当某个应用程序希望给用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知实现。使用通知的步骤1、需要一个NotificationManager来获得NotificationManager manager = (NotificationManager
    02-05 安卓开发
  • Android view系统分析-setContentView
    Android view系统分析-setContentView
    第一天上班,列了一下今年要学习的东西。主要就是深入学习Android相关的系统源代码,夯实基础。对于学习Android系统源代码,也没什么大概,就从我们平常使用最基础的东西学起,也就是从view这个切入点开始学习Android的源码,在没分析源码之前,我们有的时候
    02-05 安卓开发
  • 如何进行网络视频截图/获取视频的缩略图
    如何进行网络视频截图/获取视频的缩略图
    小编导读:获取视频的缩略图,截图正在播放的视频某一帧,是在音视频开发中,常遇到的问题。本文是主要用于点播中截图视频,同时还可以获取点播视频的缩略图进行显示,留下一个问题,如下图所示, 如果要获取直播中节目视频缩略图,该怎么做呢?(ps:直播是直
  • Android NDK 层发起 HTTP 请求的问题及解决
    Android NDK 层发起 HTTP 请求的问题及解决
    前言新的一年,大家新年快乐~~鸡年大吉!本次给大家带来何老师的最新文章~虽然何老师还在过节,但依然放心不下广大开发者,在此佳节还未结束之际,给大家带来最新的技术分享~ 事件的起因不说了,总之是需要实现一个 NDK 层的网络请求。为了多端适用,还是选择
  • SDK热更之如何在SDK代码中自动插桩及如何生成补
    写在前面本文是SDKHotfix相关的SDK热更系列文章中的一篇,以下为项目及系列文章相关链接:SDKHotfix整体介绍:http://blog.bihe0832.com/sdk_hotfix_project.htmlSDKHotfix对应github地址:https://github.com/bihe0832/SDKHoxFix这篇文章主要介绍一下SDK热更
  • 安装量破千万的第一个产品,我总结了3句话
    安装量破千万的第一个产品,我总结了3句话
    在今天的文章中,作者回顾了自己的第一个产品,他说“我做的第一款产品,是我的一块里程碑。”一起来看看~背景老牌大型互联网公司,部门内部创业的一个项目。我作为产品经理,也是第一次做产品经理,主导产品项目。实际上,项目初期包括我和安卓开发2个人。开
  • 移动周刊第 176 期:Android 知识梳理
    移动周刊第 176 期:Android 知识梳理
    写在前面 本期移动周刊第 176 期如约而至,聚焦 Android、iOS、VR/AR/MR、直播等前沿移动开发技术,收录一周最热点,解读开发技巧,每周三移动周刊抢先看,我们希望从中能够让你有一些收获,如果你有好的文章以及优化建议,请发送邮件至mobilehub@csdn.net,
  • Android插件化(六): OpenAtlasの改写aapt以防止资源ID冲突
    Android插件化(六): OpenAtlasの改写aapt以防
    引言Android应用程序的编译中,负责资源打包的是aapt,如果不对打包后的资源ID进行控制,就会导致插件中的资源ID冲突。所以,我们需要改写aapt的源码,以达到通过某种方式传递资源ID的Package ID,通过aapt打包时获取到这个Package ID并且应用才插件资源的命名
    02-05 安卓开发
  • Android架构(一)MVP架构在Android中的实践
    Android架构(一)MVP架构在Android中的实践
    为什么要重视程序的架构设计 对程序进行架构设计的原因,归根结底是为了 提高生产力 。通过设计是程序模块化,做到模块内部的 高聚合 和模块之间的 低耦合 (如依赖注入就是低耦合的集中体现)。 这样做的好处是使得程序开发过程中,开发人员主需要专注于一点,
    02-05 安卓开发
点击排行