绑定Service

一个Activity程序启动Service之后,Service就会自动留在后台运行,一般需要一个前台界面来

配置Service的运行,此时则需要将一个Activity和一个Service绑定在一起

  Ibinder是通过Service返回到Activity程序上的一个连接的绑定对象,而后这个对象要通过ServiceConnection接口进行控制。

 

绑定Service_第1张图片

 

1、先单击“启动Service”,得如下输出:

 

 

 

 

2、最后单击“绑定Service”,得如下输出:

绑定Service_第2张图片

 

1、如果不单击“启动Service”,而是直接单击“绑定Service”,得如下输出:



 

  2、最后按后退键(或者是单击“解除Service绑定”)都得如下输出:

 

绑定Service_第3张图片

 

 

在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_marginTop="8dp"

     android:layout_width="160dp"

     android:layout_height="40dp"

     android:background="#3399ff"

     android:textColor="#ffffff"

     android:text="启动Service" />

  <Button

     android:id="@+id/stop"

     android:layout_marginTop="8dp"

     android:layout_width="160dp"

     android:layout_height="40dp"

     android:background="#3399ff"

     android:textColor="#ffffff"

     android:text="停止Service" />

  <Button

     android:id="@+id/bind"

     android:layout_marginTop="8dp"

     android:layout_width="160dp"

     android:layout_height="40dp"

     android:background="#3399ff"

     android:textColor="#ffffff"

     android:text="绑定Service" />

  <Button

     android:id="@+id/unbind"

    android:layout_marginTop="8dp"

     android:layout_width="160dp"

     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.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.IBinder;

import android.os.RemoteException;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class MyServiceDemo extends Activity {

  private Button start,stop,bind,unbind ;

  private ServiceConnection serviceConnection = new ServiceConnection() {

     public void onServiceDisconnected(ComponentName name) {

       System.out.println("### Service Connect Failure");

     }

     public void onServiceConnected(ComponentName name, IBinder service) {

       try {

         System.out.println("### Service Connect Success service = " + service.getInterfaceDescriptor());

       } catch (RemoteException e) {

         e.printStackTrace();

       }

      

     }

  };

  @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()) ;

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

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

     this.bind.setOnClickListener(new BindOnClickListenerImpl());

     this.unbind.setOnClickListener(new UnBindOnClickListenerImpl());

    

    

  }

  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)) ;

     }

    

  }

  private class BindOnClickListenerImpl implements OnClickListener{

     public void onClick(View v) {

       MyServiceDemo.this.bindService(new Intent(MyServiceDemo.this,

            MyServiceUtil.class), MyServiceDemo.this

            .serviceConnection, Context.BIND_AUTO_CREATE);

      

     }

    

  }

  private class UnBindOnClickListenerImpl implements OnClickListener{

    

     public void onClick(View v) {

       MyServiceDemo.this.unbindService(MyServiceDemo.this.serviceConnection);

     }

  }

}

 

 

 

 

在MyServiceUtil.java中:

 

package com.li.service;

 

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

 

public class MyServiceUtil extends Service {

  private IBinder myBinder = new Binder(){

     @Override

     public String getInterfaceDescriptor() {

       return "MyServiceUtil Class";

     }

  };

  @Override

  public IBinder onBind(Intent intent) {

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

     return this.myBinder;  //

  }

 

  @Override

  public void onRebind(Intent intent) {

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

     super.onRebind(intent);

  }

 

  @Override

  public boolean onUnbind(Intent intent) {

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

     return super.onUnbind(intent);

  }

 

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

 

 

你可能感兴趣的:(绑定Service)