您的位置:首页 > 大数据 > 人工智能

AIDL的简单实现

2015-12-07 14:59 489 查看
package com.example.service_text;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
* @author HD
* @date 2015-12-7
* @package_name com.example.service_text
* @file_name MainActivity.java
*/
public class MainActivity extends Activity implements OnClickListener {
private Button mButton1, mButton2;
private MyAIDLService mAIDLService;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("hd", "process is" + Process.myPid());
mButton1 = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
switch (v.getId()) {
case R.id.button1:
Intent startintent = new Intent();
startintent.setClass(MainActivity.this, MyService.class);
bindService(startintent, conn, BIND_AUTO_CREATE);
break;
case R.id.button2:
Intent stopIntent = new Intent();
stopIntent.setClass(MainActivity.this, MyService.class);
unbindService(conn);
break;
}
}

ServiceConnection conn = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO 自动生成的方法存根
Log.i("hd", "onServiceDisconnected");
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO 自动生成的方法存根
Log.i("hd", "onServiceConnected");
mAIDLService = MyAIDLService.Stub.asInterface(service);
try {
int rusult = mAIDLService.plus(5, 4);
String upperStr = mAIDLService.toUpperCase("hello world");
Log.i("hd", "result is"+rusult);
Log.i("hd", upperStr);
} catch (RemoteException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
};

}


package com.example.service_text;

import com.example.service_text.MyAIDLService.Stub;

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

/**
* @author HD
* @date 2015-12-7
* @package_name com.example.service_text
* @file_name MyService.java
*/
public class MyService extends Service {

@Override
public IBinder onBind(Intent intent) {
// TODO 自动生成的方法存根

return mBinder;
}

@Override
public void onCreate() {
// TODO 自动生成的方法存根
super.onCreate();
Log.i("hd", "process is" + Process.myPid());
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO 自动生成的方法存根
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
// TODO 自动生成的方法存根
super.onDestroy();
Log.i("hd", "onDestroy");
}

MyAIDLService.Stub mBinder = new Stub() {

@Override
public String toUpperCase(String str) throws RemoteException {
// TODO 自动生成的方法存根
if (str != null) {
return str.toUpperCase();
}
return null;
}

@Override
public int plus(int a, int b) throws RemoteException {
// TODO 自动生成的方法存根
return a + b;
}
};
}


/Service_text/src/com/example/service_text/MyAIDLService.aidl

package com.example.service_text;
interface MyAIDLService{
int plus(int a,int b);
String toUpperCase(String str);

}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service_text"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
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=".MyService"
android:process=":remote" >
</service>
</application>

</manifest>


<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.example.service_text.MainActivity" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="104dp"
android:layout_marginTop="146dp"
android:text="绑定Service" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:text="解除绑定Service" />

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: