android的四大组件之一Service(服务的绑定)

怎么绑定服务,又怎么解除服务,代码如下:

MainActivity.java源码:

package com.example.lesson14_binder;

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.view.View; /**
* 2013-6-18 下午2:22:00
*
* @author 乔晓松
*/
public class MainActivity extends Activity { public MyService myService;
// 内部类获取连接对象
public ServiceConnection conn = new ServiceConnection() { // 连接失败后调用的方法
@Override
public void onServiceDisconnected(ComponentName name) {
myService = null;
System.out.println("-----service Faild");
} // 连接成功后调用的方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService = ((MyService.MyBinder) service).getService();
System.out.println("-----service connection");
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} // 绑定服务
public void bindService(View v) { Intent serviceIntent = new Intent(this, MyService.class);
this.bindService(serviceIntent, conn, Context.BIND_AUTO_CREATE); System.out.println("---------bindService");
} // 解除绑定
public void unBindService(View v) {
this.unbindService(conn);
System.out.println("---------unBindService");
} }

MyService.java源码:

package com.example.lesson14_binder;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; /**
* 2013-6-18 下午2:16:37
*
* @author 乔晓松
*/
public class MyService extends Service { public MyBinder myBinder = new MyBinder(); // 绑定的时候执行
@Override
public IBinder onBind(Intent intent) {
System.out.println("------onBind");
return myBinder;
} // 重新绑定的时候执行
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
System.out.println("------onRebind");
} // 解除绑定的时候执行
@Override
public boolean onUnbind(Intent intent) {
System.out.println("------onUnbind");
return super.onUnbind(intent);
} // 创建服务的时候执行
@Override
public void onCreate() {
super.onCreate();
System.out.println("------onCreate");
} // 开启服务的时候执行
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("------onStartCommand");
return super.onStartCommand(intent, flags, startId);
} // 销毁服务的时候执行
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("------onDestroy");
} // 继承绑定类的内部类
public class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
}

布局文件的代码:

<RelativeLayout 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: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=".MainActivity" > <Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="59dp"
android:onClick="bindService"
android:text="@string/btn_start" /> <Button
android:id="@+id/btn_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_start"
android:layout_alignRight="@+id/btn_start"
android:layout_below="@+id/btn_start"
android:layout_marginTop="58dp"
android:onClick="unBindService"
android:text="@string/btn_end" /> </RelativeLayout>

Java乔晓松-android的四大组件之一Service(服务的绑定)的更多相关文章

  1. Java乔晓松-android中调用系统拍照功能并显示拍照的图片

    android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...

  2. Java乔晓松-android中上传图片到服务器Tomcat(Struts2)

    在做android开发的时候,有时你会用到图片的上传功能,在我的android项目中,我是选中图片,点击上传多张图片 android客户端上传图片部分的代码如下: package com.exampl ...

  3. Java乔晓松-android中的帧动画FrameByFrame

    先看效果后上代码: 动画开始---- 动画切换的界面---- 动画播放完毕后的跳转界面----- 重要的方法: imageView.setBackgroundResource(R.anim.frame ...

  4. Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug

    由于android获取图片过大是会出现内存溢出的Bug 07-02 05:10:13.792: E/AndroidRuntime(6016): java.lang.OutOfMemoryError 解 ...

  5. Android开发四大组件之Service(具体解释篇)

    Android开发之四大组件--Service 一.Service 简单介绍 Service是android系统中的四大组件之中的一个(Activity.Service.BroadcastReceiv ...

  6. Android四大组件之Service --- 服务的生命周期

    一旦在项目的任何位置调用了Context的startService() 方法,相应的服务就会启动起来,并回调onStartCommand() 方法.如果这个服务之前还没有创建过,onCreate() ...

  7. Android Studio四大组件之Service

    Service在Android运行在后台,它没有可视化界面,只是默默运行在后台.我们以一个后台定时器的例子清晰的说明Service的运行流程. 一.创建Service类 项目右键->New-&g ...

  8. Android四大组件之Service

    Android四大组件之Service Android支持服务的概念,服务是在后台运行的组件,没有用户界面,Android服务可用有与活动独立的生命周期.Android支持两种类型的服务: 本地服务: ...

  9. Android的四大组件

    Android的四大组件:Activity.Service.BroadcastReceiver.Content Provider. Content Provider 属于Android应用程序的组件之 ...

随机推荐

  1. IOS不用AutoLayout也能实现自己主动布局的类(3)----MyRelativeLayout横空出世

    对于IOS开发人员来说,在自己主动布局出现前仅仅能通过计算和设置frame的值来处理.这样设置位置时就会出现非常多硬编码,同一时候在屏幕旋转和不同屏幕之间适配时须要编码又一次调整位置和尺寸,我们也能够 ...

  2. 利用用户自己的server、tomcat下的解决iOS7.1企业应用无法安装应用程序 由于证书无效的问题

    1.环境 )操作系统:Windows server 2003.Windows server2008 )JDK:jdk 1.6 )apache-tomcat-6.0.35(注意版本号号,版本号6.0.1 ...

  3. maven使用.02.一些概念

    在上一篇POST中,简要的介绍了一下maven的特点,优势,安装.并建立了一个简单地Hello world工程.这一篇POST中,将主要会介绍一下Maven的一些约定. pom.xml文件 Maven ...

  4. Webbrowser控件execcommand参数详解

    2D-Position 允许通过拖曳移动绝对定位的对象.AbsolutePosition 设定元素的 position 属性为“absolute”(绝对).BackColor 设置或获取当前选中区的背 ...

  5. Fitnesse使用系列二

    决策表 Fitnesse中提供了好几种表格样式,前面说了.表格是运行測试的关键.从字面看.表格描写叙述的是測试用例.从运行角度看,表格为后端的代码(fitnesse里称作fixture)提供了包名.类 ...

  6. hdu3329(2次dfs)

    传送门:The Flood 题意:当水的高度升为多少的时候,能够将这块区域分为两个部分. 分析:枚举高度,先从外围开始一次dfs,将水能淹没的标记,然后看非标记的是否已分为多块. #include&l ...

  7. Wix学习整理(7)——在开始菜单中为HelloWorld添加卸载快捷方式

    原文:Wix学习整理(7)--在开始菜单中为HelloWorld添加卸载快捷方式 通过前面的几篇随笔,我们已经给我们的HelloWorld提供了填写注册表信息,以及开始菜单快捷方式和桌面快捷方式.这些 ...

  8. C++ Primer中文版(第5版)

    <C++ Primer中文版(第5版)> 基本信息 作者: (美)Stanley B. Lippman(斯坦利 李普曼)    Josee Lajoie(约瑟 拉乔伊)    Barbar ...

  9. android中file的使用实例

    File是android的4种存储方式的一种.File就是文件的意思一个文件,你无非是想进行读写操作.所以这就用到两个流.一个数输入流,一个是输出流.FileOutstream,和FileInputS ...

  10. Lichee (五岁以下儿童) sysconfig1.fex 配置系统

    sysconfig配置系统,作为一个通用的软件平台,还希望通过它.能够适应用户不同的方案.通过给出一个相应的配置.用户的方案就能够自己主动执行,而不须要改动系统里面的代码,或者又一次给出參数. 一. ...