我有一壶酒 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地址的一部分进行提交,而且对字节数的 ...
随机推荐
- isset() 与 empty() 的区别
PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...
- Java多线程之~~~使用Exchanger在线程之间交换数据[这个结合多线程并行会有解决很多问题]生产者消费者模型
http://blog.csdn.net/a352193394/article/details/39503857 Java多线程之~~~使用Exchanger在线程之间交换数据[这个结合多线程并行会 ...
- hdu_5807_Keep In Touch(分段dp)
题目链接:hdu_5807_Keep In Touch 题意: 在Byteland一共有nn个城市,编号依次为11到nn,同时有mm条单向道路连接着这些城市,其中第ii条道路的起点为u_iui, ...
- shell脚本学习(二)
4.cat命令 1) cat -s 摆脱多余的空白行 2) cat -T 将制表符显示为^I 3) cat -n 显示行号 4) cat -b 跳过空白行,然后显示行号 ...
- .Net TransactionScope事务
使用TransactionScope类 正如名称所暗示,TransactionScope类用于限定事务代码块,其具有一些明显优点,例如范围与应用程序对象模型无关,同时提供了一个简单直观的编程模型等等. ...
- unix文件系统
转自here 我一向坚持的原则,那就是任何东西的根本性的,本质上的原理以及背后的思想都是及其简单的,所谓的复杂性都是优化与策略化的扩展带来的,正如TCP一样,UNIX的文件系统也不例外!我们必须知道, ...
- 校验 MD5 值
Linux 环境下:打开终端,输入命令:"md5sum filename",将结果与网页提供值对比.Windows 环境下:下载 MD5 校验软件并使用.
- 1.2 Python基本语法
1.交互模式编程 cmd窗口 =>输入 Python => 输入 print "hello,python!"; ps:如果是新版本Python,需要输 ...
- Zookeeper: configuring on centos7
thispassage is referenced, appreciated. ZooKeeper installation: Download from this site Install java ...
- Python Django Learning Notes..
The first time I came across django was last month.. Since then I was considering it as the better c ...