简介
Service是一个应用组件, 它用来在后台完成一个时间跨度比较大的工作
且没有关联任何界面
一个Service可以完成下面这些工作:
访问网络
播放音乐
文件IO操作
大数据量的数据库操作
服务的特点:
Service在后台运行,不用与用户进行交互
即使应用退出, 服务也不会停止.
在默认情况下,Service运行在应用程序进程的主线程(UI线程)中,
如果需要在Service中处理一些网络连接等耗时的操作,
那么应该将这些任务放在分线程中处理,避免阻塞用户界面
Service与Activity区别
Activity:
Activity对应一个界面
应用退出, Activity对象就会死亡
应用再次进入, 启动的Activity对象是重新创建的
Service
不与任何界面关联
应用退出, Service仍在运行
应用再次进入,
Service与Thread区别
Service
用来在后台完成一个时间跨度比较大的工作的应用组件
Service的生命周期方法运行在主线程,
如果Service想做持续时间比较长的工作, 需要启动一个分线程(Thread)
应用退出: Service不会停止
应用再次进入: 可以与正在运行的Service进行通信
Thread
用来开启一个分线程的类, 做一个长时间的工作
Thread对象的run()在分线程执行
应用退出: Thread不会停止,
应用再次进入:
Service的分类
Local Service(本地服务)
Service对象与Serive的启动者在同个进程中运行,
两者的通信是进程内通信
Remote Service(远程服务)
Service对象与Service的启动者不在同一个进程中运行,
这时存在一个进程间通信的问题,
使用本地service
定义service
定义一个类继承于Service类
public class MyService extends Service
在AndroidManifest.xml中配置Service
启动与停止Service
方式一 : 一般启动与停止
context.startService(Intent intent)
context.stopService(Intent intent)
方式二 : 绑定启动与解绑
context.bindService(Intent intent, ServiceConnection connection)
context.unbindService(ServiceConnection connection)
区别: 看Service启动后是否与启动者有关联?
Service对象经历的生命周期是否相同?
Service的生命周期
生命周期方法:
onCreate()
onStartCommand()
onBind()
onUnbind()
onDestroy()
调用的顺序:
/*
1. startService(intent)
第一次调用 :
-->构造方法()-->onCreate()-->onStartCommand()
后面再调用 : -->onStartCommand()
stopService() :
-->onDestory()
2. bindService(intent, serviceConnection)
调用 :
-->构造方法()-->onCreate()-->onBind()-->onServiceConnected()
unbindService():
(最后一个Activity与Service连接)-->onUnbind()-->onDestroy()
*/
测试例子
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1. 测试启动本地服务"
android:textSize="25sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="startMyService"
android:text="启动服务" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="stopMyService"
android:text="停止服务" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2. 测试绑定本地服务"
android:textSize="25sp"
android:layout_marginTop="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="bindMyService"
android:text="绑定服务" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="unbindMyService"
android:text="解绑服务" />
</LinearLayout>
</LinearLayout>
package com.jane.service.local;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service
{
public MyService()
{
Log.e("TAG", "MyService()");
}
@Override
public void onCreate()
{
super.onCreate();
Log.e("TAG", "MyService onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e("TAG", "MyService onStartCommand()");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.e("TAG", "MyService onDestroy()");
}
@Override
public IBinder onBind(Intent intent)
{
Log.e("TAG", "onBind()");
return new Binder();
}
@Override
public boolean onUnbind(Intent intent)
{
Log.e("TAG", "onUnbind()");
return super.onUnbind(intent);
}
}
package com.jane.service;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
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.widget.Toast;
import com.jane.service.local.MyService;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// 启动服务
public void startMyService(View v)
{
Intent intent = new Intent(this, MyService.class);
startService(intent);
Toast.makeText(this, "start service", 0).show();
}
public void stopMyService(View v)
{
Intent intent = new Intent(this, MyService.class);
stopService(intent);
Toast.makeText(this, "stop service", 0).show();
}
private ServiceConnection conn;
// 绑定服务
public void bindMyService(View v)
{
Intent intent = new Intent(this, MyService.class);
// 创建连接对象
if (conn == null)
{
conn = new ServiceConnection()
{
@Override
public void onServiceDisconnected(ComponentName name)
{
Log.e("TAG", "onServiceDisconnected()");
}
@Override
public void onServiceConnected(ComponentName name,
IBinder service)
{
Log.e("TAG", "onServiceConnected()");
}
};
// 绑定Service
bindService(intent, conn, Context.BIND_AUTO_CREATE);
Toast.makeText(this, "bind service", 0).show();
} else
{
Toast.makeText(this, "已经bindservice", 0).show();
}
}
// 解绑服务
public void unbindMyService(View v)
{
if (conn != null)
{
unbindService(conn);
conn = null;
Toast.makeText(this, "unbind service", 0).show();
} else
{
Toast.makeText(this, "还没有bindservice", 0).show();
}
}
// 在Activity死亡之前调用
@Override
protected void onDestroy()
{
super.onDestroy();
if (conn != null)
{
unbindService(conn);
conn = null;
}
}
}