Android四大组件之Service学习

来源:互联网 发布:无主之地前传改枪软件 编辑:程序博客网 时间:2024/06/04 22:31

概述:
Service是Android的四大组件之一,与Activity是同级别的,不能自己运行且没有可视化界面,可以在很多的场合下使用,如音乐播放器、检测sd卡的变化、地理位置发生改变等。

生命周期图:

这里写图片描述

服务分为两种:一种为Service,另一种为Service的抽象类IntentService。

Service启动方式分为两种:
一、Started启动方式:
* 1.服务不随Activity的销毁而销毁
* 2.服务同时只会被创建一次,可以在外部调用stopService()或者在本服务类的内部调用stopSelf()来停止该服务.
* 3.当执行一个已经启动的服务的时候,会直接调用onStartCommond()来执行功能.
* 4.Service默认情况下和Activity一样运行在同一个进程的同一个UI线程中,当再Service中执行耗时操作时,UI界面会出现明显卡住的效果,所以在onStartCommon()方法中需要开一个子线程来执行功能。
代码如下:

package com.feicui.servicetest.services;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;/** * started service: * 1.服务不随Activity的销毁而销毁 * 2.服务同时只会被创建一次,可以在外部调用stopService()或者在本服务类的内部调用stopSelf()来停止该服务. * 3.当执行一个已经启动的服务的时候,会直接调用onStartCommond()来执行功能. * 4.Service默认情况下和Activity一样运行在同一个进程的同一个UI线程中,当再Service中执行耗时操作时,UI界面会出现明显卡住的效果,所以在onStartCommon()方法中需要开一个子线程来执行功能。 * * */public class MyService extends Service {    public MyService() {    }    @Override    public IBinder onBind(Intent intent) {        // TODO: Return the communication channel to the service.        throw new UnsupportedOperationException("Not yet implemented");    }    @Override    public void onCreate() {        super.onCreate();        Log.i("Text","服务已创建");    }    /**在此方法中实现service所要实现的功能*/    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.i("Text","服务所在的线程名:"+Thread.currentThread().getName());        for (int i=0;i<50;i++){            Log.i("Text",i+"");//            try {//                Thread.sleep(500);//            } catch (InterruptedException e) {//                e.printStackTrace();//            }            if (i==30){//                this.stopSelf();                break;//需要通过break来终止循环,否则stopSelf()之后,仍会打印。            }        }        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        super.onDestroy();        Log.i("Text","服务被停止");    }}

二、Bound启动方式
* 1.通过bindService()绑定,unbindService()解绑。
* 2.与该服务进行绑定的Activity(Service、Content Provider,不能与BroadCast绑定)被杀掉时,如果没有进行解绑(unBindService()),则会报错,所以在退出前需要进行解绑操作。
* 3.通过bindService()启动的服务,能够与客户端(调用bindService()方法的地方)进行交互。
*
* 使用:客户端与Service端式通过iBinder接口进行交互的,所以需要实现一个Binder类的实例,通过onBind()返回出去。
* 当绑定成功后,在调用端(客户端)通过ServiceConnection回调接口中的onServiceConnected()方法来获得IBinder对象。
*
*
*
* Ps:该服务为本地服务,如果需要将该服务转变为远程服务,进行IPC(进程间通信):
* 1.需要在配置清单中该服务中添加android:process=”:XXX”属性
* 2.需要定义一个AIDL文件来实现进程间访问。(onBind()返回的IBinder的对象应当是一个AIDL定义的类)

package com.feicui.servicetest.services;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;/** * BindService: *  1.通过bindService()绑定,unbindService()解绑。 *  2.与该服务进行绑定的Activity(Service、Content Provider,不能与BroadCast绑定)被杀掉时,如果没有进行解绑(unBindService()),则会报错,所以在退出前需要进行解绑操作。 *  3.通过bindService()启动的服务,能够与客户端(调用bindService()方法的地方)进行交互。 * *  使用:客户端与Service端式通过iBinder接口进行交互的,所以需要实现一个Binder类的实例,通过onBind()返回出去。 *      当绑定成功后,在调用端(客户端)通过ServiceConnection回调接口中的onServiceConnected()方法来获得IBinder对象。 * * * *  Ps:该服务为本地服务,如果需要将该服务转变为远程服务,进行IPC(进程间通信): *      1.需要在配置清单中该服务中添加android:process=":XXX"属性 *      2.需要定义一个AIDL文件来实现进程间访问。(onBind()返回的IBinder的对象应当是一个AIDL定义的类) * */public class MyBoundService extends Service {    public MyBoundService() {    }    @Override    public void onCreate() {        super.onCreate();    }    @Override    public IBinder onBind(Intent intent) {        return new MyBinder();    }    @Override    public boolean onUnbind(Intent intent) {        return super.onUnbind(intent);    }    @Override    public void onDestroy() {        super.onDestroy();    }    public class MyBinder extends Binder{        public void show(){            Log.i("Text","Hello,我是一个BoundService");        }    }}

功能页面代码:

package com.feicui.servicetest;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.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import com.feicui.servicetest.services.MyBoundService;import com.feicui.servicetest.services.MyIntentService;import com.feicui.servicetest.services.MyService;public class MainActivity extends AppCompatActivity {    boolean isBind = false;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    /**启动一个服务*/    public void start(View view){        Intent intent = new Intent(this, MyService.class);        startService(intent);        Log.i("Text","Activity所在的线程名:"+Thread.currentThread().getName());    }    /**停止一个服务*/    public void stop(View view){        Intent intent = new Intent(this,MyService.class);        stopService(intent);    }    /**回调接口,用于数据的交互*/    private ServiceConnection connection = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            isBind =true;            MyBoundService.MyBinder myBinder = (MyBoundService.MyBinder) service;            myBinder.show();        }        @Override        public void onServiceDisconnected(ComponentName name) {        }    };    /**绑定一个服务*/    public void cilckBoundService(View view){        Intent intent = new Intent(this, MyBoundService.class);        bindService(intent,connection, Context.BIND_AUTO_CREATE); //Context.BIND_AUTO_CREATE:如果该服务存在则直接使用,没有则进行创建。    }    /**解绑一个服务*/    public void clickUnboundService(View view){        if (isBind) {            unbindService(connection);        }    }}

XML文件代码:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.feicui.servicetest.MainActivity">    <Button        android:text="启动一个服务"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_alignParentStart="true"        android:id="@+id/button"        android:onClick="start"/>    <Button        android:text="停止一个服务"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/button"        android:layout_alignEnd="@+id/button"        android:layout_marginTop="18dp"        android:id="@+id/button2"        android:onClick="stop"/>    <Button        android:text="启动一个绑定的服务"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/button3"        android:layout_alignParentStart="true"        android:layout_marginTop="53dp"        android:onClick="cilckBoundService"        android:id="@+id/button6"/>    <Button        android:text="解除一个绑定的服务"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/button6"        android:layout_alignEnd="@+id/button6"        android:layout_marginTop="40dp"        android:onClick="clickUnboundService"        android:id="@+id/button7"/></RelativeLayout>
0 0
原创粉丝点击