一.Service的简介
1.Service介绍和作用
Service是Android系统中的四大组件之一,它是一种长生命周期的,没有可视化界面,运行于后台的一种服务程序。比如我们播放音乐的时候,有可能想边听音乐边干些其他事情,当退出播放音乐的应用,如果不用Service,我 们就听不到歌了,所以这时候就得用到Service了。
2.Service生命周期
Service的生命周期并不像Activity那么复杂,它只继承了onCreate(),onStart(),onDestroy()三个方法,当第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法,这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法。
二.Service的启动方式
Service的有两种启动方式:Context.startService()和Context.bindService(),这两种方式对Service生命周期的影响是不同的。
1.Context.startService()方式启动
①Context.startService()方式的生命周期:
启动时,startService –> onCreate() –> onStart()
停止时,stopService –> onDestroy()
如果调用者直接退出而没有停止Service,则Service 会一直在后台运行
Context.startService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onStart()方法。如果调用startService()方法前服务已经被创建,多次调用startService()方法并不会导致多次创建服务,但会导致多次调用onStart()方法。采用startService()方法启动的服务,只能调用Context.stopService()方法结束服务,服务结束时会调用onDestroy()方法。
②Context.startService()方式启动 Service的方法:
下面是具体例子:
MainActivity.java
- package com.android.service.activity;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
-
public class MainActivity extends Activity
- {
-
private Button startBtn;
-
private Button stopBtn;
-
@Override
-
public void onCreate(Bundle savedInstanceState)
- {
-
super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- startBtn = (Button) findViewById(R.id.startBtn);
- stopBtn = (Button) findViewById(R.id.stopBtn);
-
- startBtn.setOnClickListener(listener);
- stopBtn.setOnClickListener(listener);
- }
-
-
-
private OnClickListener listener=new OnClickListener()
- {
-
@Override
-
public void onClick(View v)
- {
-
Intent intent=new Intent(MainActivity.this, ServiceDemo.class);
-
switch (v.getId())
- {
-
case R.id.startBtn:
- startService(intent);
-
break;
-
case R.id.stopBtn:
- stopService(intent);
-
break;
-
default:
-
break;
- }
- }
- };
- }
ServiceDemo.java
- package com.android.service.activity;
-
-
import android.app.Service;
-
import android.content.Intent;
-
import android.os.IBinder;
-
import android.util.Log;
-
-
public class ServiceDemo extends Service
- {
-
private static final String TAG="Test";
-
-
@Override
-
-
public void onCreate()
- {
-
Log.i(TAG, "Service onCreate--->");
-
super.onCreate();
- }
-
-
@Override
-
-
public void onStart(Intent intent, int startId)
- {
-
Log.i(TAG, "Service onStart--->");
-
super.onStart(intent, startId);
- }
-
-
@Override
-
-
public void onDestroy()
- {
-
Log.i(TAG, "Service onDestroy--->");
-
super.onDestroy();
- }
-
-
@Override
-
-
public IBinder onBind(Intent intent)
- {
-
return null;
- }
- }
-
main.xml
- <?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<Button
-
android:id="@+id/startBtn"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:text="启动 Service"
-
/>
-
<Button
-
android:id="@+id/stopBtn"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:text="停止 Service"
-
/>
-
</LinearLayout>
在AndroidManifest.xml文件中添加16~21行的声明
- <?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-
package="com.android.service.activity"
-
android:versionCode="1"
-
android:versionName="1.0">
-
<uses-sdk android:minSdkVersion="10" />
-
-
<application android:icon="@drawable/icon" android:label="@string/app_name">
-
<activity android:name=".MainActivity"
-
android:label="@string/app_name">
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
-
<service android:name=".ServiceDemo" >
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</service>
-
</application>
-
</manifest>
效果图:
当点击按钮时,先后执行了Service中onCreate()->onStart()这两个方法,LogCat显示如下:
当点击 按钮时,Service则执行了onDestroy()方法,LogCat显示如下:
当点击或按钮,进入Settings(设置)->Applications(应用)->Running Services(正在运行的服务)看一下我们新启动了的服务,效果图如下:
2.Context.bindService()方式启动:
①Context.bindService()方式的生命周期:
绑定时,bindService -> onCreate() –> onBind()
调用者退出了,即解绑定时,Srevice就会unbindService –>onUnbind() –> onDestory()
用Context.bindService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onBind()方法。这个时候调用者和服务绑定在一起,调用者退出了,系统就会先调用服务的onUnbind()方法,接着调用onDestroy()方法。如果调用bindService()方法前服务已经被绑定,多次调用bindService()方法并不会导致多次创建服务及绑定(也就是说onCreate()和onBind()方法并不会被多次调用)。如果调用者希望与正在绑定的服务解除绑定,可以调用unbindService()方法,调用该方法也会导致系统调用服务的onUnbind()-->onDestroy()方法。
②Context.bindService()方式启动 Service的方法:
绑定Service需要三个参数:bindService(intent, conn, Service.BIND_AUTO_CREATE);
第一个:Intent对象
第二个:ServiceConnection对象,创建该对象要实现它的onServiceConnected()和 onServiceDisconnected()来判断连接成功或者是断开连接
第三个:如何创建Service,一般指定绑定的时候自动创建
下面是具体的例子:
MainActivity.java
- package com.android.bindservice.activity;
-
-
import android.app.Activity;
-
import android.app.Service;
-
import android.content.ComponentName;
-
import android.content.Intent;
-
import android.content.ServiceConnection;
-
import android.os.Bundle;
-
import android.os.IBinder;
-
import android.util.Log;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
-
public class MainActivity extends Activity {
-
-
private Button startBtn,stopBtn,bindBtn,unbindBtn;
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
- setContentView(R.layout.main);
-
- startBtn = (Button)findViewById(R.id.startBtn1);
- stopBtn = (Button)findViewById(R.id.stopBtn2);
- bindBtn = (Button)findViewById(R.id.bindBtn3);
- unbindBtn = (Button)findViewById(R.id.unbindBtn4);
-
-
- startBtn.setOnClickListener(startListener);
- stopBtn.setOnClickListener(stopListener);
- bindBtn.setOnClickListener(bindListener);
- unbindBtn.setOnClickListener(unBindListener);
-
- }
-
-
private OnClickListener startListener = new OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
-
Intent intent = new Intent();
-
-
intent.setClass(MainActivity.this, BindService.class);
-
- startService(intent);
- }
- };
-
-
-
private OnClickListener stopListener = new OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
-
Intent intent = new Intent();
-
-
intent.setClass(MainActivity.this, BindService.class);
-
- stopService(intent);
- }
- };
-
-
-
private ServiceConnection conn = new ServiceConnection() {
-
@Override
-
public void onServiceConnected(ComponentName name, IBinder service) {
-
Log.i("Service", "连接成功!");
- }
-
@Override
-
public void onServiceDisconnected(ComponentName name) {
-
Log.i("Service", "断开连接!");
- }
- };
-
-
-
private OnClickListener bindListener = new OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
-
Intent intent = new Intent();
-
-
intent.setClass(MainActivity.this, BindService.class);
-
-
- bindService(intent, conn, Service.BIND_AUTO_CREATE);
- }
- };
-
-
-
private OnClickListener unBindListener = new OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
-
Intent intent = new Intent();
-
-
intent.setClass(MainActivity.this, BindService.class);
-
- unbindService(conn);
- }
- };
- }
BindService.java
- package com.android.bindservice.activity;
-
-
import android.app.Service;
-
import android.content.Intent;
-
import android.os.IBinder;
-
import android.util.Log;
-
-
public class BindService extends Service{
-
-
private static final String TAG="Test";
-
-
-
public IBinder onBind(Intent intent) {
-
Log.i(TAG, "Service onBind--->");
-
return null;
- }
-
-
public void onCreate() {
-
Log.i(TAG, "Service onCreate--->");
- }
-
-
public void onStart(Intent intent, int startId) {
-
Log.i(TAG, "Service onStart--->");
- }
-
-
public void onDestroy() {
-
Log.i(TAG, "Service onDestroy--->");
- }
-
-
public boolean onUnbind(Intent intent) {
-
Log.i(TAG, "Service onUnbind--->");
-
return super.onUnbind(intent);
- }
- }
-
main.xml
- <?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<Button
-
android:text="启动Service"
-
android:id="@+id/startBtn1"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
/>
-
<Button
-
android:text="停止Service"
-
android:id="@+id/stopBtn2"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
/>
-
<Button
-
android:text="绑定Service"
-
android:id="@+id/bindBtn3"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
/>
-
<Button
-
android:text="解除绑定"
-
android:id="@+id/unbindBtn4"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
/>
-
</LinearLayout>
在AndroidManifest.xml文件中添加16~21行的声明
- <?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-
package="com.android.bindservice.activity"
-
android:versionCode="1"
-
android:versionName="1.0">
-
<uses-sdk android:minSdkVersion="10" />
-
-
<application android:icon="@drawable/icon" android:label="@string/app_name">
-
<activity android:name=".MainActivity"
-
android:label="@string/app_name">
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
-
<service android:name=".BindService" >
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</service>
-
</application>
-
</manifest>
效果图:
当点击按钮时,先后执行了Service中onCreate()->onStart()这两个方法,LogCat显示如下:
当点击按钮,则Service执行了onUnbind()方法,LogCat显示如下:
当点击按钮,则Service执行了onUnbind()方法,LogCat显示如下:
本文转自 lingdududu 51CTO博客,原文链接:http://blog.51cto.com/liangruijun/647804