ANDROID_MARS学习笔记_S01原始版_019_SERVICE之Transact

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

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.transact.MainActivity" >
10 
11     <Button
12         android:id="@+id/btn1"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="绑定" />
16      <Button
17         android:id="@+id/btn2"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="发送请求" 
21         android:layout_below="@id/btn1"/>
22 
23 </RelativeLayout>

 

(2)AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.transact"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="21" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name=".MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <service android:name=".ThirdService"></service>
26     </application>
27 
28 </manifest>

 

2.java
(1)MainActivity.java

 1 package com.transact;
 2 
 3 import android.app.Activity;
 4 import android.content.ComponentName;
 5 import android.content.Intent;
 6 import android.content.ServiceConnection;
 7 import android.os.Binder;
 8 import android.os.Bundle;
 9 import android.os.IBinder;
10 import android.os.Parcel;
11 import android.os.RemoteException;
12 import android.view.View;
13 import android.view.View.OnClickListener;
14 import android.widget.Button;
15 
16 public class MainActivity extends Activity {
17 
18     private Button btn1,btn2;
19     private Binder binder;
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         btn1 = (Button) findViewById(R.id.btn1);
25         btn2 = (Button) findViewById(R.id.btn2);
26         btn1.setOnClickListener(new OnClickListener() {
27             @Override
28             public void onClick(View v) {
29                 Intent intent = new Intent();
30                 intent.setClass(MainActivity.this, ThirdService.class);
31                 bindService(intent, conn, BIND_AUTO_CREATE);
32             }
33         });
34         
35         btn2.setOnClickListener(new OnClickListener() {
36             @Override
37             public void onClick(View v) {
38                 Parcel data = Parcel.obtain();
39                 Parcel reply = Parcel.obtain();
40                 data.writeString("data from activity");
41                 try {
42                     //Transact()会等待service的onTransact()方法往reply写数据,service的onTransact()执行完后会返回这里
43                     binder.transact(0, data, reply, 0);
44                     System.out.println("activity's replya data -->"+reply.readString());
45                 } catch (RemoteException e) {
46                     e.printStackTrace();
47                 }
48             }
49         });
50     }
51     
52     ServiceConnection conn = new ServiceConnection() {
53         @Override
54         public void onServiceDisconnected(ComponentName name) {
55             
56         }
57         
58         @Override
59         public void onServiceConnected(ComponentName name, IBinder service) {
60             MainActivity.this.binder = (Binder) service;
61         }
62     };
63 }

 

(2)ThirdService.java

 

 1 package com.transact;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.Binder;
 6 import android.os.IBinder;
 7 import android.os.Parcel;
 8 import android.os.RemoteException;
 9 
10 public class ThirdService extends Service {
11 
12     @Override
13     public IBinder onBind(Intent intent) {
14         return new MyBinder();
15     }
16 
17     class MyBinder extends Binder {
18         @Override
19         protected boolean onTransact(int code, Parcel data, Parcel reply,
20                 int flags) throws RemoteException {
21             System.out.println("code---->"+code);
22             System.out.println("flags---->"+flags);
23             System.out.println("data---->"+data.readString());
24             reply.writeString("add data from thirdService");
25             return super.onTransact(code, data, reply, flags);
26         }
27     }
28 }

 

posted @ 2016-02-16 18:00  shamgod  阅读(334)  评论(0编辑  收藏  举报
haha