我有一壶酒 Android学习之Service(1)--->BinderService方式








本文只讨论扩展Binder类







创建一个Binder.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"
>
<Button
android:id="@+id/btnStartBinderService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start BinderService"
/>
<Button
android:id="@+id/btnStopBinderService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop BinderService"
/>
</LinearLayout>
创建一个BinderService.jvaa类,继承Service
package com.szy.service; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class BinderService extends Service
{
private static final String TAG = "BinderService";
private MyBinder binder =new MyBinder(); public class MyBinder extends Binder
{
public BinderService getService()
{
return BinderService.this;
}
}
@Override
public IBinder onBind(Intent intent)
{
return binder;
} public void MyMethod()
{
Log.i(TAG, "MyMethod()");
} }
再新建一个类BinderActivity.java继承Activity
package com.szy.service; import com.szy.service.BinderService.MyBinder; 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;
import android.view.View.OnClickListener;
import android.widget.Button; public class BinderActivity extends Activity
{
private Button btnStartBinderService;
private Button btnStopBinderService; private Boolean isConnected = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.binder);
btnStartBinderService=(Button)findViewById(R.id.btnStartBinderService);
btnStopBinderService=(Button)findViewById(R.id.btnStopBinderService);
btnStartBinderService.setOnClickListener(listener);
btnStopBinderService.setOnClickListener(listener);
} private OnClickListener listener=new OnClickListener()
{ public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnStartBinderService:
bindService();
break;
case R.id.btnStopBinderService:
unBind();
break;
default:
break;
}
} }; private void unBind()
{
if (isConnected)
{
unbindService(conn);
}
} private void bindService()
{
Intent intent=new Intent(BinderActivity.this, BinderService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
} private ServiceConnection conn=new ServiceConnection()
{ public void onServiceDisconnected(ComponentName name)
{
isConnected=false;
} public void onServiceConnected(ComponentName name, IBinder binder)
{
MyBinder myBinder= (MyBinder)binder;
BinderService service=myBinder.getService();
service.MyMethod();
isConnected=true; }
};
}
修改下AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.szy.service"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
</activity> <activity android:name=".BinderActivity"
android:label="@string/app_name">
</activity> <activity android:name=".IntentActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <service android:name=".ExampleService" />
<service android:name=".BinderService" />
<service android:name=".MyService"/>
<service android:name=".ExampleIntentService"/>
</application>
</manifest>
我有一壶酒 Android学习之Service(1)--->BinderService方式的更多相关文章
- Android学习之Service(1)--->Started方式
界面退出后进程程序还在运行,不会被杀死,如音乐播发器.后台下载等 注:本文只讨论Started方式 main.xml代码分析 <?xml version="1.0" enco ...
- Android学习总结——Service组件
从Service的启动方式上,可以将Service分为Started Service和Bound Service.在使用Service时,要想系统能够找到此自定义Service,无论哪种类型,都需要在 ...
- Android 学习笔记 Service服务与远程通信...(AIDL)
PS:这一章节看的我有几分迷茫,不是很容易理解...不过还好总算是明白了一大半了...基本的迷惑是解决了... 学习内容: 1.跨应用启动服务... 2.跨应用绑定服务... 3.跨应用实现通信... ...
- Android 学习笔记 Service
PS:前几篇的内容光是上代码了,也没有细细的讲解..感觉这样写很不好..因此还是多一些讲解吧... 学习内容: 1.了解Service... 2.Service的启动与停止.. 3.绑定与取消绑定Se ...
- 【Android学习】Service&Boradcast初步
Service初步 掌握Service概念 掌握Service分类 Service开发能力具备 了解Service和intentService类的区别 重点难点 StartService和BoundS ...
- 【Android学习】四种布局方式
一.LinearLayout 线性布局,即一行展开或者一列展开,也可以嵌套,需要注意的属性如下: android:orentation //对齐方式 二.FrameLayout 帧布局,即一层层叠起 ...
- android学习笔记 Service
Service(服务): 长期后台运行的没有界面的组件 android应用什么地方需要用到服务? 天气预报:后台的连接服务器的逻辑,每隔一段时间获取最新的天气信息.股票显示:后台的连接服务器的逻辑,每 ...
- Android学习笔记④——页面的布局方式
FrameLayout(帧布局) 这个布局的特点是简单的默认把每一个视图组件都放在边框内且放在左上角,即使添加多个视图组件,他们也都是重叠在左上角,新的视图会遮挡住旧的视图.可以根据gravity来改 ...
- Android学习之Http使用Post方式进行数据提交(普通数据和Json数据)
转自:http://blog.csdn.net/wulianghuan/article/details/8626551 我们知道通过Get方式提交的数据是作为Url地址的一部分进行提交,而且对字节数的 ...
随机推荐
- 观看网上的N多教程有感
MD只想说一句,我擦. 长篇大论,有个叼毛用呀,显示你文采.... 糟粕真TMD多,直接简单的步骤多好,不要显示的你有多专业,其实就是一个二逼. 还有N多论坛,扯淡的人更多.
- C语言介绍
以下东东转自百度百科 C语言是一种计算机程序设计语言,它既具有高级语言的特点,又具有汇编语言的特点.它由美国贝尔实验室的Dennis M. Ritchie于1972年推出,1978年后,C语言已先后被 ...
- 十三章:使用WEB字体
1.WEB字体可以使用一系列文件类型,下面介绍三种字体类型: (1)内嵌OpenType (2)TrueType和OpenType台式机使用的标准字体文件类型 (3)WEB开放字体格式. 2.构造子集 ...
- 【Machine Learning in Action --2】K-近邻算法构造手写识别系统
为了简单起见,这里构造的系统只能识别数字0到9,需要识别的数字已经使用图形处理软件,处理成具有相同的色彩和大小:宽高是32像素的黑白图像.尽管采用文本格式存储图像不能有效地利用内存空间,但是为了方便理 ...
- jsoup抓取数据
jsoup的主要功能如下: 1. 从一个URL,文件或字符串中解析HTML: 2. 使用DOM或CSS选择器来查找.取出数据: 3. 可操作HTML元素.属性.文本: 接下来介绍jsoup 是如何优雅 ...
- java中的Unicode中文转义
String ori = "\u5e7f\u4e1c"; public static String convertUnicode(String ori) { char aChar; ...
- APUE读书笔记:进程控制
重点函数:fork,exit,_exit 一.fork 函数原型: #include <unistd.> pid_t fork(void) 函数说明:fork函数将创建一个子进程,该函数调 ...
- php数据排序---array_multisort
PHP代码 <?php $ar1 = array(10, 100, 100, 0); $ar2 = array(1, 3, 2, 4); array_multisort($ar1, $ar2); ...
- 老鸟需要知道的一些php系统类函数
作为一个老手级别的php程序员,知道下面几个php系统级别的函数,不足为多吧!获取系统信息和调试程序的时候应该能用的上! PHP系统类函数 assert函数:检查assertion声明是否错误 ext ...
- C++设计模式-Visitor访问者模式
#include <iostream> #include <string> #include <string.h> #include <memory> ...