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应用程序的组件之 ...
随机推荐
- javascripte (三) 改变html图像
<script> function changeImage(){ element=document.getElementById("myimage") if (elem ...
- C#对HTTP数据还原
使用C#对HTTP数据还原 [创建时间:2016-05-12 00:19:00] NetAnalyzer下载地址 在NetAnalyzer2016中加入了一个HTTP分析功能,很过用户对此都很感兴 ...
- linux下 文件IO 相关
linux下操作文件或设备,需要一个文件描述符 file descriptor,fd 来引用.fd是一个非负整数,实际上是一个索引值,指向文件的记录表,对文件的操作都需要fd.默认的几个:标准输入流 ...
- c#Lamdba表达式与托付
介绍: "Lambda表达式"(lambda expression)是一个匿名函数,在C#3.0中引入了lambda表达式,它是对匿名函数的一种简化,能够包括表达式和语句,而且可用 ...
- haproxy 中的http请求和https请求
use Mojolicious::Lite; use JSON qw/encode_json decode_json/; use Encode; no strict; use JSON; # /foo ...
- 图像编程学习笔记1——bmp文件结构处理与显示
文本内容转载自<数字图像处理编程入门>,代码为自己实现 1.1图和调色板的概念 如今Windows(3.x以及95,98,NT)系列已经成为绝大多数用户使用的操作系统,它比DOS成功的一个 ...
- 字符串string和内存流MemoryStream及比特数组byte[]互转
原文:字符串string和内存流MemoryStream及比特数组byte[]互转 字符串string和内存流MemoryStream及比特数组byte[]互转比较 定义string变量为str, ...
- 基于S5pv210流媒体server的实现之网络摄像头(by liukun321 咕唧咕唧)
这里仅介绍流媒体server端的实现思路.及编码注意问题,不会贴代码的详细实现. 直接入正题先介绍一下系统硬件框架: server端连接PC机用VLC播放例如以下图: server端应用程序能够分为图 ...
- 实现TextView 文字排版,分散两端对齐
參考:http://www.cnblogs.com/lcyty/p/3265335.html 方法一:使用HTML TextView textview=(TextView)findViewbyId(R ...
- C++基础知识---static const初始化成员变量
为了限制常数的范围class中.你必须要做出成为class成员:而要确保这是丝毫不亚于有一个恒定的实体.你必须要做出成为static员: Class Gameplayer { Private: Sta ...