安卓开发_浅谈Service

一、Service(服务)

Service是Android程序中四大基础组件之一,它和Activity一样都是Context的子类,区别在于它没有UI界面,是在后台运行的组件。

public abstract class
Service
extends ContextWrapper
implements ComponentCallbacks java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.app.Service

二、Service启动方法+相应的生命周期

Service的生命周期并不是固定的,而是要看启动Service的方式。

而启动Service的方式又分为两种startService和bindService

安卓开发_浅谈Service

1、 StartService(启动运行在后台的服务,所谓后台即没有界页;作为四大组件之一,其是运行在主线程中的)

启动时:

Context.startService(intent)-->onCreate()àonStartCommand ()

停止时:

Context_stopService(intent)-->onDestroy()

使用方法:

(1)、创建一个自定义服务类继承Service,实现抽象方法

(2)、清单文件中注册自定义的服务类

(3)、在activity中通过startService和 stopService()

看一个Demo

 package com.example.demo01;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View; public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); } public void btn_startService(View view)
{
Intent intent = new Intent(this,MyService.class);
intent.putExtra("info", "这里是传送的数据");
startService(intent);
Log.i("activity", "开启服务"); }
public void btn_closeService(View view)
{
Intent intent = new Intent(this,MyService.class);
Log.i("activity", "关闭服务");
stopService(intent);
}
}

MainActivity.class

 package com.example.demo01;

 import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log; public class MyService extends Service{ @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("MyService", "--->onCreate"); } @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i("MyService", "--->onBind"); return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("MyService", "--->onStartCommand"); String str = intent.getStringExtra("info");
Log.i("MyService", "自定义服务MyService被activity启动并收到了activity发送过来信息:"+str); return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("MyService", "--->onDestroy");
} }

MyService.class自定义服务类

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <Button
android:onClick="btn_startService"
android:text="开启服务并发送消息"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> </Button>
<Button
android:onClick="btn_closeService"
android:text="关闭服务"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> </Button>
</LinearLayout>

activity_main.xml 布局文件

清单文件注册自定义的服务类

         <service android:name="com.example.demo01.MyService"></service>

点击两次开启服务按钮,然后点击一次关闭服务按钮,

可见第一次开启服务的时候执行onCreate()服务, 再执行onStartCommand()方法,

当第二次开启服务的时候,则不再执行onCreate()方法,直接执行onStartCommand

当关闭服务的时候,执行onDestroy()方法

安卓开发_浅谈Service

2、BindService(基于IBinder方式将两个组件进行绑定,然后相互传值,如果以绑定的方式启动的服务,在解除绑定时也会自动停止服务)

绑定时:

bindService-->onCreate()-->onBind()

解绑时:

unbindService-->onUnbind()-->onDestory()

使用方法:

(1)、创建一个自定义服务类继承Service,实现onBind()方法

(2)、创建Bindler的子类

(3)、在onBind()方法中返回自定义Bindler子类的对象

(4)、清单文件中组册自定义服务

(5)、创建ServiceConnection接口对象,实现 onServiceConnected()方法和 onServiceDisconnected方法

(6)、在activity中绑定bindService和解绑服务unbindService

Demo

 package com.example.demo02;

 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{ private MyBinder binder ; @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i("service", "-->onBind");
return new MyBinder();
} @Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i("service", "-->onCreate");
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("service", "-->onStartCommand");
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i("service", "-->onDestroy");
super.onDestroy();
} class MyBinder extends Binder{
public void function(){
Log.i("service", "Binder-->function"); } } }

MyService.class

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定服务"
android:onClick="btn_bindService"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解绑服务"
android:onClick="btn_unbindService"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="执行自定义Binder子类的function方法"
android:onClick="btn_Binder"
/> </LinearLayout>

activity_main.xml 布局文件

清单文件注册:

        <service android:name="com.example.demo02.MyService"></service>

一共三个按钮

当点击“绑定服务”按钮时 执行

安卓开发_浅谈Service

当点击“执行自定义Binder子类的function方法”按钮时 执行

安卓开发_浅谈Service

当点击“解绑服务”按钮时 执行

安卓开发_浅谈Service

BIND_AUTO_CREATE标识表示:绑定的服务组件如果不存,则会自动创建,
由bindService方式启动的Service,其生命周期会受到绑定组件的影响,即当绑定组件Activity销毁时,Service也会停止

上一篇:跟vczh看实例学编译原理——三:Tinymoe与无歧义语法分析


下一篇:使用MATLAB生成模糊控制的离线查询表