Service详解一
16lz
2021-01-26
MainActivity:
package com.phone.day23_servicedemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.View;/** * Service :****** * 服务像 Windows上面的服务一样,服务是在后台上运行,承担着静悄悄的不为人所注意的工作。 * Service运行在后台,它是不可见的、无界面的程序。 * * 没有页面的进程 应用场景: 播放音乐,接收广播,下载图片 等等 * * 概念总结:***** * 1.Service在后台运行,不可以与用户直接交互 * 2.一个服务不是一个单独的进程 * 3.一个服务不是一个单独的线程。Service和其他组件一样,默认情况下,Service中的所有代码都是运行在主线程中 (可验证) * 4。需要通过某一个Activity或其他Context对象来启动Service * * Service很大程度上充当了应用程序后台线程管理器的角色。(如果Activity中新开启一个线程,当该Acitivyt关闭后,该线程依然在工作, * 但是与开启它的Activity失去联系。也就是说此时的这个线程处于失去管理的状态。但是使用Service,则可以对后台运行的线程有效地管理。) * * 为什么使用Service 而不使用 Thread***** * 1、Service可以放在独立的进程中,所以更安全; * 2、使用Service可以依赖现有的binder机制,不需要在应用层面上处理线程同步的繁杂工作 (BindService 的时候讲解) * 3、系统可以重新启动异常死去的Service。 * * 与Activity***** * 1.不同点:Activity是与用户交互的组件,即可以看到UI界面,而Service是在后台运行、无需界面 * 2.相同点:使用Activity 时我们需要在配置文件中声明标签, 同样的使用Service * 也需要在配置文件中声明标签。都具有一定的生命周期。 * * 分类:本地服务与远程服务:***** * A:本地服务 * 一、本地服务通过启动方式分为两种: * 1.启动服务Started Service ***** * 被启动的服务是由其它组件调用startService()方法而启动的,该方法会导致被启动服务的生命周期方法onStartCommand()被回调。 * 当服务是被启动状态后,其生命周期与启动它的组件无关,即使启动服务的组件(Activity,BroadcastReceiver)已经被销毁, * 该服务还可以在后台无限期运行。除非调用stopSelf()或stopService()来停止该服务 * 2.绑定服务Bound Service xxxxxxxxx * TODO * 二、 根据方法onStartCommand 的返回值分为三种**** * 1.START_STICKY 常量1 粘性 * 与应用程序 粘 在一起 如果执行结束onStartCommand 后 服务被异常杀死之后还会再次重新启动 ,但是 参数为null * 游乐场发生了意外 醒来失意 * 2.START_NOT_STICKY 常量2 非粘性 * 被异常杀死之后 不会重新启动了 * 发生了意外 没醒来 * 3.START_REDELIVER_INTENT 常量3 * 系统会重新启动这个服务并将Intent值传入 * 醒来 有记忆 * B:远程服务xxxxx * TODO * * Service 生命周期********* * 有个生命周期的图 * 一、.Started Service的生命周期*********** * * onCreate服务被创建 * onStartCommand 服务开始运行 onstart 2.0 以前 * onDestroy 服务销毁 * * 说明: * 1.在程序中调用:context.startService()会触发执行Service生命周期中的onCreate()、onStartCommand()回调方法,此时服务就开始正式运行 * 2.如果Service还没有运行,则android先调用onCreate()然后调用onStartCommand();如果Service已经运行,则只调用onStartCommand (), * 所以一个Service的onStartCommand方法可能会重复调用多次; * 3.如果在程序中调用:context.stopService()会触发执行Service生命周期中的onDestroy()回调方法,会让服务停止 * 4.stopService()的时候直接onDestroy,如果是调用者自己直接退出而没有调用stopService()的话,Service会一直在后台运行。 * 该Service的调用者再启动该Service后可以通过stopService关闭Service;stopSelf() * 5.所以StartService的生命周期为:onCreate --> onStartCommand(可多次调用) --> onDestroy * * 二、.Bound Service的生命周期 XXXXXXX * TODO * * ********************* * 备注: * 1.Service是不能自己启动的,只有通过 Context 对象调用startService() 和bindService() 方法来启动 * 2.Service每一次的开启关闭过程中,只有onStartCommand()可被多次调用(通过多次startService调用), * 3.Service可以在和多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity这个时候程序要在后台继续播放, * 比如检测SD卡上文件的变化,再或者在后台记录你地理信息位置的改变等等,总之服务总是藏在后头的 * * */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view) { switch (view.getId()) { case R.id.start_bt: // 启动Service //在通过Intent跳转(Service和Activity的跳转一样)的同时,可以跟Intent添加数据进行传递// Intent intent =new Intent(this, MyService.class);// intent.putExtra("key", path); startService(new Intent(this, MyService.class)); break; case R.id.end_bt: // 停止 stopService(new Intent(this, MyService.class)); break; default: break; } }}
MyService:
package com.phone.day23_servicedemo;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class MyService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /** * 创建服务 */ @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.d("TAG", "______onCreate_____"); } /** * * 执行后 Service处于 isRunning 状态 * 第一个参数:启动的时候传递的Intent 对象 * 第二个参数:标记 * 第三个参数:服务的id * * * * 返回值: * START_STICKY * 如果当前服务被异常杀死。系统会重新启动该服务,intent = null * START_NOT_STICKY * 如果当前服务被一场杀死,系统不会重新启动该服务 * START_REDELIVER_INTENT * 如果当前服务被异常杀死。系统会重新启动该服务,并且将Intent 的值重新传入 * START_STICKY_COMPATIBILITY 了解: * 他是START_STICKY 的兼容版本: 如果当前服务被异常杀死,系统不一定会重新启动该服务 */ @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Log.d("TAG", "______onStartCommand_____");// 所有代码都是运行在主线程中 int id = (int) Thread.currentThread().getId(); String name = Thread.currentThread().getName(); Log.d("TAG", "name___" + name + "____id__" + id); // 自杀的方法// stopSelf(); return START_STICKY; } //销毁服务 @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.d("TAG", "______onDestroy_____"); }}
更多相关文章
- tcping测试服务器TCP端口
- Android通过广播接收者调用服务内方法
- Android(安卓)开发游戏中的SurfaceView,Callback,SurfaceHolder
- setonClickListener()的解释
- Android开发-打开另一个Activity或打开网页
- handler机制(二)源码分析
- android 上传Bitmap到服务器端
- Android动态加载启动页
- Android数据库升级、降级、创建(onCreate() onUpgrade() onDowng