Skip to content

Commit

Permalink
使用aidl小案例
Browse files Browse the repository at this point in the history
  • Loading branch information
atguiguafu committed Jan 11, 2017
1 parent 84af07d commit ae5e6f3
Show file tree
Hide file tree
Showing 13 changed files with 410 additions and 0 deletions.
19 changes: 19 additions & 0 deletions binderService/build.gradle
@@ -0,0 +1,19 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "com.yanguangfu.binder"
minSdkVersion 14
targetSdkVersion 23
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
27 changes: 27 additions & 0 deletions binderService/src/main/AndroidManifest.xml
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yanguangfu.binder"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />

<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity android:name="com.yanguangfu.binder.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name="com.yanguangfu.binder.MyService" >
<!-- <intent-filter>
<action android:name="com.yanguangfu.binder.action.AIDLService" />
</intent-filter>-->
</service>
</application>

</manifest>
@@ -0,0 +1,6 @@
package com.yanguangfu.binder.aidl;
import com.yanguangfu.binder.aidl.Rect1;

interface AIDLActivity {
void performAction(in Rect1 rect);
}
@@ -0,0 +1,9 @@
package com.yanguangfu.binder.aidl;
import com.yanguangfu.binder.aidl.AIDLActivity;
interface AIDLService {
void registerTestCall(AIDLActivity cb);
void invokCallBack();

String getName();
int getAge();
}
@@ -0,0 +1,2 @@
package com.yanguangfu.binder.aidl;
parcelable Rect1;
153 changes: 153 additions & 0 deletions binderService/src/main/java/com/yanguangfu/binder/MainActivity.java
@@ -0,0 +1,153 @@
package com.yanguangfu.binder;

import java.text.MessageFormat;

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.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.yanguangfu.binder.aidl.AIDLActivity;
import com.yanguangfu.binder.aidl.AIDLService;
import com.yanguangfu.binder.aidl.Rect1;

/**
*
* @author 杨光福
*
*/
public class MainActivity extends Activity implements OnClickListener {
private Button btn_bindService;
private Button btn_unbindService;
private Button btnCallBack;
private Button btn_get_data;



@Override
public void onCreate(Bundle icicle) {
Log.e("yangguangfu", "MainActivity.onCreate");
super.onCreate(icicle);
setContentView(R.layout.activity_main);
//初始化布局文件
initView();
//设置点击事件
setClickListener();
}

/**
* 设置点击事件
*/
private void setClickListener() {
btn_bindService.setOnClickListener(this);
btn_unbindService.setOnClickListener(this);
btnCallBack.setOnClickListener(this);
btn_get_data.setOnClickListener(this);
}

/**
* 初始化布局文件
*/
private void initView() {
btn_bindService = (Button) findViewById(R.id.btn_bindService);
btn_unbindService = (Button) findViewById(R.id.btn_unbindService);
btnCallBack = (Button) findViewById(R.id.btn_call_back);
btn_get_data = (Button) findViewById(R.id.btn_get_data);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_bindService:
Log.e("yangguangfu", "MainActivity.点击了btn_bindService");

Intent intent = new Intent(this,MyService.class);
//绑定服务
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
//启动服务
startService(intent);
break;
case R.id.btn_unbindService:
Log.e("yangguangfu", "MainActivity.点击了btn_unbindService");
unbindService(mConnection);
// stopService(intent);
break;
case R.id.btn_call_back:

Log.e("yangguangfu", "MainActivity.点击了btn_unbindService");
try {
mService.invokCallBack();
} catch (RemoteException e) {
e.printStackTrace();
}

break;
case R.id.btn_get_data:

Log.e("yangguangfu", "MainActivity.点击了btn_get_data");
if(mService==null){
Toast.makeText(getApplicationContext(), "服务还没有绑定", 0).show();
return ;
}

try {
String dataFromService = mService.getName()+"---"+mService.getAge();
Log.e("yangguangfu", "MainActivity.dataFromService=="+dataFromService);
Toast.makeText(getApplicationContext(), dataFromService, 0).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

break;


default:
break;
}

}



private AIDLActivity mCallback = new AIDLActivity.Stub() {

@Override
public void performAction(Rect1 rect) throws RemoteException {
Log.e("yangguangfu", "MainActivity.performAction");
Log.e("yangguangfu", MessageFormat.format(
"MainActivity.rect[bottom={0},top={1},left={2},right={3}]", rect.bottom,
rect.top, rect.left, rect.right));
Toast.makeText(MainActivity.this,
"这个土司是由Service回调Activity弹出来的", 1).show();

}
};

private AIDLService mService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
Log.e("yangguangfu", "MainActivity.onServiceConnected");
mService = AIDLService.Stub.asInterface(service);
try {
mService.registerTestCall(mCallback);
} catch (RemoteException e) {

}
}

public void onServiceDisconnected(ComponentName className) {
Log.e("yangguangfu", "MainActivity.onServiceDisconnected");
mService = null;
}
};
}
106 changes: 106 additions & 0 deletions binderService/src/main/java/com/yanguangfu/binder/MyService.java
@@ -0,0 +1,106 @@
package com.yanguangfu.binder;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.yanguangfu.binder.aidl.AIDLActivity;
import com.yanguangfu.binder.aidl.AIDLService;
import com.yanguangfu.binder.aidl.Rect1;

/**
*
* @author 杨光福
*
*/
public class MyService extends Service {

private AIDLActivity callback;



@Override
public void onCreate() {
Log.e("yangguangfu", "MyService.onCreate");
}


@Override
public void onStart(Intent intent, int startId) {
Log.e("yangguangfu", "MyService.onStart startId="+startId);
}


@Override
public IBinder onBind(Intent t) {
Log.e("yangguangfu", "MyService.onBind");
return mBinder;
}


@Override
public void onDestroy() {
Log.e("yangguangfu", "MyService.onDestroy");
super.onDestroy();
}


@Override
public boolean onUnbind(Intent intent) {
Log.e("yangguangfu", "MyService.onUnbind");
return super.onUnbind(intent);
}


public void onRebind(Intent intent) {
Log.e("yangguangfu", "MyService.onRebind");
super.onRebind(intent);
}

private String getNames(){
Log.e("yangguangfu", "MyService.getName");
return "name from service";
}

private int getAges(){
Log.e("yangguangfu", "MyService.getAge");
return 24;
}

private final AIDLService.Stub mBinder = new AIDLService.Stub() {

@Override
public void invokCallBack() throws RemoteException {
Log.e("yangguangfu", "MyService.AIDLService.invokCallBack");
Rect1 rect = new Rect1();
rect.bottom=-1;
rect.left=-1;
rect.right=1;
rect.top=1;
callback.performAction(rect);
}


@Override
public void registerTestCall(AIDLActivity cb) throws RemoteException {
Log.e("yangguangfu", "MyService.AIDLService.registerTestCall");
callback = cb;
}


@Override
public String getName() throws RemoteException {
Log.e("yangguangfu", "MyService.AIDLService.getName");
return getNames();
}


@Override
public int getAge() throws RemoteException {
Log.e("yangguangfu", "MyService.AIDLService.getAge");
return getAges();
}
};
}
48 changes: 48 additions & 0 deletions binderService/src/main/java/com/yanguangfu/binder/aidl/Rect1.java
@@ -0,0 +1,48 @@
package com.yanguangfu.binder.aidl;

import android.os.Parcel;
import android.os.Parcelable;

public class Rect1 implements Parcelable {
public int left;
public int top;
public int right;
public int bottom;
public static final Parcelable.Creator<Rect1> CREATOR = new Parcelable.Creator<Rect1>() {
public Rect1 createFromParcel(Parcel in) {
return new Rect1(in);
}

public Rect1[] newArray(int size) {
return new Rect1[size];
}
};

public Rect1() {
}

private Rect1(Parcel in) {
readFromParcel(in);
}

public void readFromParcel(Parcel in) {
left = in.readInt();
top = in.readInt();
right = in.readInt();
bottom = in.readInt();
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(left);
out.writeInt(top);
out.writeInt(right);
out.writeInt(bottom);
}

}
Binary file added binderService/src/main/res/drawable-hdpi/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added binderService/src/main/res/drawable-ldpi/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added binderService/src/main/res/drawable-mdpi/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ae5e6f3

Please sign in to comment.