声明:各个方法的用法都在代码的注释里:可以自行观看

Service的代码:

public class MyService extends Service {MyBinder myBinder = new MyBinder();@Nullable@Overridepublic IBinder onBind(Intent intent) {//放回的Ibinder是一个接口,放回Binder对象(继承了IBinder)也可以//返回的Binder会传入到ServiceConnection的重写方法onServiceConnected中Log.d("MyService" , "onBind: ");return myBinder;}@Overridepublic void onRebind(Intent intent) {super.onRebind(intent);Log.d("MyService" , "onRebind: ");//当onUnbind的返回值为true时,与该服务绑定的活动离开视线后再重新回到视线后,重新bindService时会调用}@Overridepublic boolean onUnbind(Intent intent) {Log.d("MyService" , "onUnbind: ");return true;}@Overridepublic void onCreate() {//在onBind或onStartCommand前调用super.onCreate();Log.d("MyService" , "onCreat: ");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d("MyService" , "onStatrCommand: ");return super.onStartCommand(intent, flags, startId);//startService时调用}@Overridepublic void onDestroy() {super.onDestroy();Log.d("MyService" , "onDestory: ");//stopService时调用}
}

layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:orientation="vertical"><Buttonandroid:onClick="sendService"android:id="@+id/bt1"android:text="sendService"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:onClick="stopService"android:id="@+id/bt2"android:text="stopService"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:onClick="bindService"android:id="@+id/bt3"android:text="bindService"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:onClick="unbindService"android:id="@+id/bt4"android:text="unbindService"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:onClick="sendIntentService"android:id="@+id/bt5"android:text="sendIntentService"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>

IntentService:解决耗时工作–在子线程中运行,结束会自动回调ondestory()


//为了实现多线程操作一些耗时的时候忘记stopService时的IntentService
//该类的功能时实现一些耗时工作后并自动关闭服务
public class MyIntentService extends IntentService {//一定要有无参构造函数,且要super一下父类的带参构造函数public MyIntentService() {super("MyIntentService");}@Overrideprotected void onHandleIntent(@Nullable Intent intent) {//此方法在子线程中运行,可以处理一下耗时的工作,具体逻辑自己写Log.d("MyIntentService", "onHandleIntent: ");}//onHandleIntent执行结束后会自动调用onDestory()@Overridepublic void onDestroy() {super.onDestroy();Log.d("MyIntentService", "onDestroy: ");}
}

Binder:


public class MyBinder extends Binder {//在绑定的时候充当onBind的返回类型public void startDownload(){Log.d("MyService", "startDownload: ");}public void getProgress(){Log.d("MyService", "getProgress: ");}
}

主函数:

public class MainActivity extends AppCompatActivity {MyBinder myBinder;Intent intent;Intent intentService;ServiceConnection serviceConnection = new ServiceConnection(){@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.d("MyService", "onServiceConnected: ");myBinder = (MyBinder) service;myBinder.getProgress();myBinder.startDownload();}@Overridepublic void onServiceDisconnected(ComponentName name) {Log.d("MyService", "onServiceDisconnected: ");}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);intent = new Intent(this, MyService.class);intentService = new Intent(this,MyIntentService.class);}public void sendService(View view) {startService(intent);}public void stopService(View view) {stopService(intent);}public void bindService(View view) {bindService(intent,serviceConnection,0);}public void unbindService(View view) {unbindService(serviceConnection);}public void sendIntentService(View view) {startService(intentService);}
}

注:MyService中的onBind方法的返回值IBinder会给ServiceConnection中的onServiceConnected()方法,可以利用该对象实现IBinder里的方法

附件:onRebind什么时候调用–>转载自https://blog.csdn.net/fenggering/article/details/53116311

Android 后台服务(Service)相关推荐

  1. Android后台服务Service

    后台服务 桌面应用程序:可见 服务:不可见  长期在后台运行 帮助应用执行耗时的操作 安卓的服务:安卓四大组件之一  不可见   后台长期运行 界面与服务有时候要执行数据交互 如何创建服务: 1. 创 ...

  2. Android——后台服务

    Android应用编程实验 实验名称:Android 后台服务 实验目的:通过Service设计后台服务程序,通过Broadcast实现信息广播机制 实验内容: 设计一个简单的后台音乐服务程序: 设计 ...

  3. Android后台服务---无交互时的Service

    Service是Android四大组件之一,他主要负责后台服务,长时间运行. 一:主要特点 (1)优先级大于Activity,即使Activity被停止后,他也不会轻易被终止 (2)即使被系统终止,在 ...

  4. android 后台服务拍照,Android实现后台开启服务默默拍照功能

    本文实例为大家分享了Android后台开启服务默默拍照的具体代码,供大家参考,具体内容如下 最近项目原因,需要编写一后台运行的程序,在给定时间间隔下进行拍照,关键技术主要是:1.开启服务:2.在不不预 ...

  5. Android移动开发之【Android实战项目】后台服务Service

    桌面应用程序:可见 服务:不可见 长期在后台运行 帮助应用执行耗时的操作 安卓的服务:安卓四大组件之一 不可见 后台长期运行 界面与服务有时候要执行数据交互 文章目录 如何创建服务 创建一个类 继承S ...

  6. Android判断后台服务(Service)是否运行

    今天在项目中需要判断某个服务是否在后台运行,因此写了一个工具类方便大家调用,话不多说,上代码. *** 服务工具类* * @author Administrator* */ public class ...

  7. android 后台运行service实现和后台的持续交互

    在项目中有这么一种需求 需要后台开启服务,时刻记录用户和软件的交互行为,一旦交互发生,就向服务器测发送一条消息 解决方案: 一.创建一个service服务类 在service中开启一个线程,servi ...

  8. Android的服务Service

    Android学了太久了,都忘了.复习下四大组件之一的Service. 介绍 Android的Service是一种在后台执行长时间运行操作的组件,它可以在没有用户界面的情况下执行任务,并且可以与应用程 ...

  9. android创建标题栏,【Android】利用服务Service创建标题栏通知

    创建标题栏通知的核心代码 public void CreateInform() { //定义一个PendingIntent,当用户点击通知时,跳转到某个Activity(也可以发送广播等) Inten ...

最新文章

  1. MonkeyRunner实例及使用说明
  2. 关于const 修饰的一些知识
  3. 使用变量_在 Linux 中使用变量 | Linux 中国
  4. 实验楼 1. k-近邻算法实现手写数字识别系统--《机器学习实战 》
  5. 计算机复试比重低的学校,又有985院校发布调剂信息,这个34所降低复试比重!...
  6. mybatis学习(1):【持久化框架】Mybatis简介与原理
  7. python中parse是什么_Python中optparse模块使用浅析
  8. JavaScript中setInterval的参数传递个人归纳
  9. knife4j--api请求参数不一致问题
  10. mysql分页优化方法
  11. HTML与CSS案例——人物简介
  12. 计算机网络网线制作教案,制作网线教案.doc
  13. 华为U8500刷了2.2后自定义铃声,短信通知音,闹铃音的方法
  14. mybatis+spring+springmvc ssm整合
  15. 代码提交到GitHub时出现的反复报错
  16. 自我激励的有效方法20个(推荐)
  17. 优秀笔记软件盘点—好看、强大的可视化笔记软件、知识图谱工具
  18. word公式编号及交叉引用技巧
  19. 技术里的故事里的技术
  20. WebSupergoo 11.310 C#PDF库组件 WebSuper

热门文章

  1. ImageExpert
  2. linux6.5退出文档,Centos6.5及Linux基础命令和基础知识点笔记!6.25更新
  3. 微信小程序-简历信息显示
  4. String简单介绍
  5. 【博学谷学习记录】超强总结,用心分享 | JavaSE入门基础知识总结
  6. matlab导出图片为pdf时警告图窗太大解决方法
  7. 《我的团长我的团》:你要让我来啊,那个犊子不愿意来呀!
  8. 前端案例 ——注册页面(html+css实现)
  9. c#报错 :System . Invalid Operation Exception:“线程间操作无效: 从不是创建控件的线程访问它
  10. SVN客户端安装及使用说明