Android详细教程(基础篇):二十七、Android Service详解

Service

Android第四大组件:Service

  • 掌握Service与Activity的区别
  • 掌握Service的定义及使用
  • 可以使用SerivceConnection接口绑定一个Service
  • 了解系统提供的Service程序。

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

10.1. Service的基本组成

在Android系统开发之中,Services是一个重要的组成部分。如果现在某些程序中的部分操作是很消耗时间的,那么可以将这些程序定义在Service之中,这样就可以完成程序的后台运行(也可以在不显示界面的形式下运行),即:Services实际上就相当于是一个没有图形界面的Activity程序,而且当用户要执行某些操作需要进行跨进程访问的时候也可以使用Service来完成。

 

继承结构:

java.lang.Object

   

android.content.Context

 

   

android.content.ContextWrapper

 

 

   

android.app.Service

 

 

Service的基本组成:

Service是一个没有UI界面的操作组件,主要的功能是为Activity程序提供一些必要的支持,例如:手机中的MP3播放软件,当回到桌面上的时候这些组件依然可以运行呢,实际上这些就属于Service的功能,在开发时用户只需要继承自android.app.Service类就可以完成Service程序的开发,在Service之中也有自己的生命周期方法。

10.2. Service的生命周期

如果要想实现Service操作的话,也需要定义Service的子类,这个子类也要覆写Service中的相应方法。

10.3. Service也是一种组件,所以,它也需要在AndroidMainfest.xml文件中配置

示例:

组件配置

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

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".ServiceActivity" >

    <Button

        android:id="@+id/startService"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="启动Service" />

    <Button

        android:id="@+id/stopService"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="停止Service" />

 

</LinearLayout>

Service服务:

package com.makyan.service;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

public class ServiceUtil extends Service {

            @Override

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

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

                       return super.onStartCommand(intent, flags, startId);

            }

            public void onDestroy() {

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

            }

            public IBinder onBind(Intent arg0) {

                       return null;

            }

}

Activity:

package com.makyan.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;

import com.example.serviceproject1.R;

import com.makyan.service.ServiceUtil;

public class ServiceActivity extends Activity {

            private Button startBut;

            private Button stopBut;

            @Override

            protected void onCreate(Bundle savedInstanceState) {

                       super.onCreate(savedInstanceState);

                       setContentView(R.layout.activity_service);

                       startBut = (Button) super.findViewById(R.id.startService);

                       stopBut = (Button) super.findViewById(R.id.stopService);

                       startBut.setOnClickListener(new StartOnClickListener());

                       stopBut.setOnClickListener(new StopOnClickListener());

            }

            class StartOnClickListener implements OnClickListener{

                       public void onClick(View arg0) {

                                   ServiceActivity.this.startService(new Intent(ServiceActivity.this,ServiceUtil.class));

                       }

            }

            class StopOnClickListener implements OnClickListener{

                       public void onClick(View arg0) {

                                   ServiceActivity.this.stopService(new Intent(ServiceActivity.this, ServiceUtil.class));

                       }

            }

}

AndroidMainfest.xml配置:

<?xml version="1.0" encoding="utf-8"?>

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

    package="com.example.serviceproject1"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="17"

        android:targetSdkVersion="17" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.makyan.activity.ServiceActivity"

            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="com.makyan.service.ServiceUtil"></service>

    </application>

</manifest>

10.4. Service完全解析

10.4.1. Service的基本用法

关于Service最基本的用法自然就是如何启动一个Service了,启动Service的方法和启动Activity很类似,都需要借助Intent来实现,

下面我们就通过一个具体的例子来看一下。

新建一个Android项目,项目名就叫ServiceTest,这里我选择使用4.0的API。

然后新建一个MyService继承自Service,并重写父类的onCreate()、onStartCommand()和onDestroy()方法,如下所示:

public class MyService extends Service {

         public static final String TAG = "MyService";

         @Override

         public void onCreate() {

                  super.onCreate();

                  Log.d(TAG, "onCreate() executed");

         }

         @Override

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

                  Log.d(TAG, "onStartCommand() executed");

                  return super.onStartCommand(intent, flags, startId);

         }

         @Override

         public void onDestroy() {

                  super.onDestroy();

                  Log.d(TAG, "onDestroy() executed");

         }

         @Override

         public IBinder onBind(Intent intent) {

                  return null;

         }

}

可以看到,我们只是在onCreate()、onStartCommand()和onDestroy()方法中分别打印了一句话,并没有进行其它

任何的操作。

然后打开或新建activity_main.xml作为程序的主布局文件,代码如下所示:

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

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <Button

        android:id="@+id/start_service"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Start Service" />

    <Button

        android:id="@+id/stop_service"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Stop Service" />

</LinearLayout>

我们在布局文件中加入了两个按钮,一个用于启动Service,一个用于停止Service。

然后打开或新建MainActivity作为程序的主Activity,在里面加入启动Service和停止Service的逻辑,代码如下所示:

 

public class MainActivity extends Activity implements OnClickListener {

            private Button startService;

            private Button stopService;

            @Override

            protected void

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值