Java乔晓松-android的四大组件之一Service(服务的绑定)
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(服务的绑定)的更多相关文章
- Java乔晓松-android中调用系统拍照功能并显示拍照的图片
android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...
- Java乔晓松-android中上传图片到服务器Tomcat(Struts2)
在做android开发的时候,有时你会用到图片的上传功能,在我的android项目中,我是选中图片,点击上传多张图片 android客户端上传图片部分的代码如下: package com.exampl ...
- Java乔晓松-android中的帧动画FrameByFrame
先看效果后上代码: 动画开始---- 动画切换的界面---- 动画播放完毕后的跳转界面----- 重要的方法: imageView.setBackgroundResource(R.anim.frame ...
- Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug
由于android获取图片过大是会出现内存溢出的Bug 07-02 05:10:13.792: E/AndroidRuntime(6016): java.lang.OutOfMemoryError 解 ...
- Android开发四大组件之Service(具体解释篇)
Android开发之四大组件--Service 一.Service 简单介绍 Service是android系统中的四大组件之中的一个(Activity.Service.BroadcastReceiv ...
- Android四大组件之Service --- 服务的生命周期
一旦在项目的任何位置调用了Context的startService() 方法,相应的服务就会启动起来,并回调onStartCommand() 方法.如果这个服务之前还没有创建过,onCreate() ...
- Android Studio四大组件之Service
Service在Android运行在后台,它没有可视化界面,只是默默运行在后台.我们以一个后台定时器的例子清晰的说明Service的运行流程. 一.创建Service类 项目右键->New-&g ...
- Android四大组件之Service
Android四大组件之Service Android支持服务的概念,服务是在后台运行的组件,没有用户界面,Android服务可用有与活动独立的生命周期.Android支持两种类型的服务: 本地服务: ...
- Android的四大组件
Android的四大组件:Activity.Service.BroadcastReceiver.Content Provider. Content Provider 属于Android应用程序的组件之 ...
随机推荐
- Go成功的项目
nsq:bitly开源的消息队列系统,性能非常高,目前他们每天处理数十亿条的消息docker:基于lxc的一个虚拟打包工具,能够实现PAAS平台的组建.packer:用来生成不同平台的镜像文件,例如V ...
- Spring MVC+JSP实现三级联动
jsp代码 <script type="text/javascript"> $(function() { initProvinces(); }); /** * 获取省列 ...
- Keywords Search (ac 自己主动机)
Keywords Search Problem Description In the modern time, Search engine came into the life of everybod ...
- thinkphp URL规则、URL伪静态、URL路由、URL重写、URL生成(十五)
原文:thinkphp URL规则.URL伪静态.URL路由.URL重写.URL生成(十五) 本章节:详细介绍thinkphp URL规则.URL伪静态.URL路由.URL重写.URL生成 一.URL ...
- 《Python学习手册》读书笔记
之前为了编写一个svm分词的程序而简单学了下Python,觉得Python很好用,想深入并系统学习一下,了解一些机制,因此开始阅读<Python学习手册(第三版)>.如果只是想快速入门,我 ...
- VSTO 学习笔记(十一)开发Excel 2010 64位自定义公式
原文:VSTO 学习笔记(十一)开发Excel 2010 64位自定义公式 Excel包含很多公式,如数学.日期.文本.逻辑等公式,非常方便,可以灵活快捷的对数据进行处理,达到我们想要的效果.Exce ...
- Windows 8.1下 MySQL绿色版安装配置与使用
原文:Windows 8.1下 MySQL绿色版安装配置与使用 Mysql-5.6.17-winx64操作步骤: 一.安装MySQL数据库 1.下载. 下载地址:http://downloads.my ...
- Spring集成XFire开发WebService
Spring是眼下最流行的JavaEE Framework,可是使用Spring的Spring-WS开发WebService却十分繁琐.XFire是一个简化WebService开发的开源项目.通过Sp ...
- cisco san交换机配置
1.配置交换机的管理地址switch(config)# interface mgmt 0 switch(config-if)# ip adress 192.168.100.108 255.255.25 ...
- CSS selectors for Selenium with example,selenium IDE
CSS selectors for Selenium with example http://seleniumeasy.com/selenium-tutorials/css-selectors-tut ...