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

时间:2021-09-11 08:11:42 

Service概念及用途:

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

Service生命周期 :

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

Service与Activity通信:

Service后端的数据最终还是要呈现在前端Activity之上的,因为启动Service时,系统会重新开启一个新的进程,这就涉及到不同进程间通信的问题了(AIDL),当我们想获取启动的Service实例时,我们可以用到bindService和unBindService方法,它们分别执行了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 onStart(Intent intent, int startId) {
  Log.i(TAG, "onstart");
  super.onStart(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&apos;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
投稿

猜你喜欢

  • android图片处理之让图片变成圆形

    2021-08-01 00:29:53
  • Android 高仿微信语音聊天页面高斯模糊(毛玻璃效果)

    2021-09-27 18:56:46
  • Java详细分析梳理垃圾回收机制

    2023-10-30 04:02:33
  • Java并发编程系列之LockSupport的用法

    2022-04-07 06:48:58
  • Netty序列化深入理解与使用

    2023-05-24 20:13:07
  • 详解spring cloud Feign使用中遇到的问题总结

    2023-12-13 19:03:48
  • 听说用了YYYY-MM-dd的程序员,前些天都在加班改Bug

    2023-07-05 17:48:00
  • Android控件gridview实现单行多列横向滚动效果

    2022-09-11 10:53:49
  • spring-boot @Component和@Bean的区别详解

    2023-06-28 16:10:36
  • Java实现俄罗斯方块的源码分享

    2023-02-17 04:20:48
  • Java技巧函数方法实现二维数组遍历

    2023-09-12 23:25:00
  • SpringBoot深入分析运行原理与功能实现

    2022-01-03 14:48:43
  • RocketMQ之Consumer整体介绍启动源码分析

    2022-06-04 03:53:31
  • 一篇文章弄懂Spring MVC的参数绑定

    2023-09-17 01:01:21
  • c# 实现文件上传下载功能的实例代码

    2021-12-10 15:00:30
  • Java实现添加条形码到PDF表格的方法详解

    2023-04-26 12:37:25
  • java实现的DES加密算法详解

    2022-10-01 09:51:11
  • IDEA全量替换一次性解决旧项目并将所有文件换行符改为LF问题

    2022-09-17 18:44:32
  • 基于Java字符编码的使用详解

    2023-02-26 23:13:01
  • C# dump系统lsass内存和sam注册表详细

    2021-06-26 12:19:53
  • asp之家 软件编程 m.aspxhome.com