绑定Service

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

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

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

 

 

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

 

 

 

 

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



 

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



 

  2、最后按后退键(或者是单击“解除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_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>

 

 

代码转载自:https://pan.quark.cn/s/7f503284aed9 Hibernate的核心组件总数达到五个,具体包括:Session、SessionFactory、Transaction、Query以及Configuration。 这五个核心组件在各类开发项目中都具有普遍的应用性。 借助这些组件,不仅可以高效地进行持久化对象的读取与存储,还能够实现事务管理功能。 接下来将通过图形化的方式,逐一阐述这五个核心组件的具体细节。 依据所提供的文件内容,可以总结出以下几个关键知识点:### 1. SSH框架详细架构图尽管标题提及“SSH框架详细架构图”,但在描述部分并未直接呈现关于SSH的详细内容,而是转向介绍了Hibernate的核心接口。 然而,在此我们可以简要概述SSH框架(涵盖Spring、Struts、Hibernate)的核心理念及其在Java开发中的具体作用。 #### Spring框架- **定义**:Spring框架是一个开源架构,其设计目标在于简化企业级应用的开发流程。 - **特点**: - **分层结构**:该框架允许开发者根据实际需求选择性地采纳部分组件,而非强制使用全部功能。 - **可复用性**:Spring框架支持创建可在不同开发环境中重复利用的业务逻辑和数据访问组件。 - **核心构成**: - **核心容器**:该部分包含了Spring框架的基础功能,其核心在于`BeanFactory`,该组件通过工厂模式运作,并借助控制反转(IoC)理念,将配置和依赖管理与具体的应用代码进行有效分离。 - **Spring上下文**:提供一个配置文件,其中整合了诸如JNDI、EJB、邮件服务、国际化支持等企业级服务。 - **Spring AO...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值