天天看点

Service服务学习(SimpleRandomServiceDemo)

Android四大组件:(以下是官方文档)

1.Activitys:

2.Services:

3.Broadcast Receivers

4.Content Providers

Activities

An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.

Services

A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.

Content providers

Broadcast receivers

什么是Service:

A service can run in the background to perform work even while the user is in a different application

使用Services的方式:

1.startService();

2.bindService();

官方文档:

Started

Bound

当然了,要想使用Service肯定是少不了在全局文件中注册Service,否则没权限

小笔记:

很让我郁闷的是为什么我的ServiceText工程中的R.java为什么不自动更新了,导致我在Activity中定位控件资源时定位不到?     V_V….

还有就是我写的SimpleRandomServiceDemo为什么老是强制关闭呢?

Inter Process Communication,IPC(进程间通信,吼吼。),它可以用于解决两个不同Android应用程序进程之间的调用和通信问题。

Service生命周期 :

调用startService()启动Service --> onCreate() --> onStart() --> Service正在运行-->Sercice被停止-->onDestroy()-->关闭Service

使用方式有两种 :

1.启动方式(不能获取Service对象,无法调用Service中的方法)

1.1:Context.startService()启动Servide

1.2:Context.stopService()终止Service或Service.stopSefl()停止Service

1.3:1.2中的终止并未真正终止,除非它所在的Activity调用onDestroy()才算真正终止

2.绑定方式(能获取Service对象,可以调用Service中的方法,通过Connection实现)

          2.1:Context.bindService()建立服务链接

2.2:Context.unbindService()终止链接

最主要的是:同一个Service可以绑定多个服务链接,这样可以同时为多个不同的组件提供服务

例:播放MP3暂停

Context.startService();启动服务

Context.bindService();获取Service对象保存播放信息,再用:暂停函数暂停播放

需要注意的是:在此情况下如何调用Context.stopService()终止播放的话并不能停止Service,需要在所有的服务链接关闭后,Service才能真正停止

Intent广播消息时候要有一个唯一标识,此处,Service在全局文件中注册时也要一个唯一标识

<service android:name=".继承Service的类名">

代码实现Service后有两种方式启动服务:显式启动和隐式启动

1.显式启动:

final Intent serviceIntent = new Intent(this,继承Service的类名.class);

startService(serviceIntent);

2.隐式启动:

final Intent serviceIntent = new Intent();

serviceIntent.setAction("继承Service的类_完整路径");

<intent-filter>

<action android:name="继承Service的类_完整路径"/>

</intent-filter>

</service>

无论何种方式 :关闭方式都一样的:stopService(serviceIntent);……………….吼吼

     本文转自华华世界 51CTO博客,原文链接:http://blog.51cto.com/mzh3344258/733361,如需转载请自行联系原作者

继续阅读