Service的基本组成

时间:2023-03-08 21:53:19

Service与Activity的最大区别就是一有界面,一个没有界面。

如果某些程序操作很消耗时间,那么可以将这些程序定义在Service之中,这样就可以完成程序的后台运行,

其实Service就是一个没有界面的Activity,执行跨进程访问也可以使用Service完成。

Service是一个没有UI界面的操作组件,主要功能是为Activity程序提供一些必要的支持,例如:手机

中的Mp3播放软件,当回到桌面的时候这些组件依然在运行,这就属于Service的功能,Service也有

自己的生命周期方法。

Service的生命周期控制方法:

方法及常量

类型

描述

1

START_CONTINUATION_MASK

常量

解析执行Service

2

START_STICKY

常量

用于显示启动和停止Service

3

IBinder onBind(Intent intent)

普通

设置Activity和Service之间的绑定

4

onCreate()

普通

当一个Service创建时调用

5

onStartCommand(Intent intent,int flags,int startId)

普通

启动Service,由startService()方法触发

6

onDestroy()

普通

Service销毁时调用,由stopService()方法触发

Service基本组成

Service的基本组成

1、单击“启动Serivce”按钮得下图:

Service的基本组成

2、再单击“启动Serivce”按钮得下图:

Service的基本组成

3、单击“停止Serivce”按钮得下图:

Service的基本组成

如果不退出Service,按Home键回到桌面,再找到任务管理器,可以发现Service还在运行,

所以说Service是运行在后台的。

Service的基本组成

在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"

android:background="#000000"

android:gravity="center_horizontal">

<Button

android:id="@+id/start"

android:layout_margin="30dp"

android:layout_width="100dp"

android:layout_height="40dp"

android:background="#3399ff"

android:textColor="#ffffff"

android:text="启动Service" />

<Button

android:id="@+id/stop"

android:layout_margin="30dp"

android:layout_width="100dp"

android:layout_height="40dp"

android:background="#3399ff"

android:textColor="#ffffff"

android:text="停止Service" />

</LinearLayout>

在MyServiceDemo.java中:

package com.li.service;

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 MyServiceDemo extends Activity {

private Button start ;

private Button stop ;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.main);

this.start = (Button) super.findViewById(R.id.start) ;

this.stop = (Button) super.findViewById(R.id.stop) ;

this.start.setOnClickListener(new StartOnClickListenerImpl()) ;

this.stop.setOnClickListener(new StopOnClickListenerImpl()) ;

}

private class StartOnClickListenerImpl implements OnClickListener{

public void onClick(View v) {

MyServiceDemo.this.startService(new Intent(MyServiceDemo.this,MyServiceUtil.class)) ;

}

}

private class StopOnClickListenerImpl implements OnClickListener{

public void onClick(View v) {

MyServiceDemo.this.stopService(new Intent(MyServiceDemo.this,MyServiceUtil.class)) ;

}

}

}

在MyServiceUtil.java中:

package com.li.service;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

public class MyServiceUtil extends Service {

@Override

public IBinder onBind(Intent intent) {

return null;  // 此处暂时不做任何的处理

}

@Override

public void onCreate() {

System.out.println("*** Service onCreate()") ;

}

@Override

public void onDestroy() {

System.out.println("*** Service onDestroy()") ;

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

System.out.println("*** Service onStartCommand") ;

return Service.START_CONTINUATION_MASK; // 继续执行

}

}

修改AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.li.service"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="15" />

<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name=".MyServiceDemo"

android:label="@string/title_activity_my_service_demo" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<service android:name=".MyServiceUtil" />

</application>

</manifest>