一、代码
1.xml
(1)activity_main.xml

 <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="com.transact.MainActivity" > <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送请求"
android:layout_below="@id/btn1"/> </RelativeLayout>

(2)AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.transact"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".ThirdService"></service>
</application> </manifest>

2.java
(1)MainActivity.java

 package com.transact;

 import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private Button btn1,btn2;
private Binder binder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ThirdService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
}); btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeString("data from activity");
try {
//Transact()会等待service的onTransact()方法往reply写数据,service的onTransact()执行完后会返回这里
binder.transact(0, data, reply, 0);
System.out.println("activity's replya data -->"+reply.readString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
} ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) { } @Override
public void onServiceConnected(ComponentName name, IBinder service) {
MainActivity.this.binder = (Binder) service;
}
};
}

(2)ThirdService.java

 package com.transact;

 import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException; public class ThirdService extends Service { @Override
public IBinder onBind(Intent intent) {
return new MyBinder();
} class MyBinder extends Binder {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
System.out.println("code---->"+code);
System.out.println("flags---->"+flags);
System.out.println("data---->"+data.readString());
reply.writeString("add data from thirdService");
return super.onTransact(code, data, reply, flags);
}
}
}

ANDROID_MARS学习笔记_S01原始版_019_SERVICE之Transact的更多相关文章

  1. ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast

    一.代码 1.xml(1)radio.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...

  2. ANDROID_MARS学习笔记_S01原始版_004_TableLayout

    1.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...

  3. ANDROID_MARS学习笔记_S01原始版_003_对话框

    1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...

  4. ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用

    一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

  5. ANDROID_MARS学习笔记_S01原始版_001_Intent

    一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...

  6. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词

    一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...

  7. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词

    一.流程分析 1.点击播放按钮,会根据lrc名调用LrcProcessor的process()分析歌词文件,得到时间队列和歌词队列 2.new一个hander,把时间队列和歌词队列传给自定义的线程类U ...

  8. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3

    一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过 ...

  9. ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签

    一.简介 1.在main.xml中用TabHost.TabWidget.FrameLayout标签作布局 2.在MainActivity中生成TabHost.TabSpec,调用setIndicato ...

随机推荐

  1. 如何在ANDROID JNI 的C++中打Log

    http://blog.csdn.net/pkigavin/article/details/8583537 最近在研究Android 2.3.3源代码的C/C++层,需要对代码进行一些调试,但是奇怪的 ...

  2. java学习笔记3

    今天在练习java中的javabean的使用中发现.usebean的 <jsp:setProperty property="id" name="user" ...

  3. linux系统文件属性

    1  硬链接概念 硬链接是指通过索引节点(Inode)来进行链接,在Linux(ext2,ext3)文件系统中,保存在磁盘分区中的文件不管是什么类型都会给它分配一个编号,这个编号被称为索引节点编号(I ...

  4. IIS6.0、IIS7中的站点、应用程序和虚拟目录详细介绍

    这里说的不是如何解决路径重写或者如何配置的问题,而是阐述一下站点(site),应用程序(application)和虚拟目录 (virtual directory)概念与作用,已及这三个东西在IIS6与 ...

  5. cocos2d-x实战 C++卷 学习笔记--第4章 使用标签

    前言: 介绍cocos2d-x中 标签类. cocos2d-x中 标签类 主要有三种:LabelTTF, LabelAtlas, 和 LabelBMFont.此外,在Cocos2d-x 3.x之后推出 ...

  6. 03静态链表_StaticLinkList--(线性表)

    #include "string.h" #include "ctype.h" #include "stdio.h" #include &qu ...

  7. 2017 google Round C APAC Test 题解

    题解参考网上的答案,以及我自己的想法. 主要参考网站:http://codeforces.com/blog/entry/47181,http://codeforces.com/blog/entry/4 ...

  8. 事件兼容IE

    addEvent:function(target, functionref, tasktype) { if (target.addEventListener) target.addEventListe ...

  9. 关闭一个winform窗体刷新另外一个

    例如Form1是你的主窗体,然后Form2是你的要关闭那个窗体,在Form1中SHOW FORM2的窗体那里加上一句f2.FormClosed += new FormClosedEventHandle ...

  10. IIS tilde directory enumeration 漏洞以及解决方案

    2015年6月16日15:19:24  出现 IIS tilde directory enumeration 漏洞 Acunetix Web Vulnerability Scanner 9.5 测试出 ...