您的位置:首页 > 移动开发 > Android开发

android_59_service_aidl调用远程服务

2017-01-10 23:02 411 查看
app_1提供远程服务



清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sg31.alipayservice"
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=".PayService">
<intent-filter >
<action android:name="com.sg31.alipayservice"/>
</intent-filter>
</service>

</application>

</manifest>


OpenInterface.aidl:

package com.sg31.alipayservice;

interface OpenInterface {
void pay();
}


服务:

package com.sg31.alipayservice;

import com.sg31.alipayservice.OpenInterface.Stub;

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

public class PayService extends Service {

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MiddleClass();
}

// 中间人对象
class MiddleClass extends Stub {

@Override
public void pay() throws RemoteException {
// 调用服务的pay方法
PayService.this.inner_pay();
}

}

public void inner_pay() {
System.out.println("检测运行环境");
System.out.println("加密用户名密码");
System.out.println("建立连接");
System.out.println("上传数据");
System.out.println("完成支付");
}
}


app_2 通过AIDL调用远程服务:



清单:

布局:

主控制器:

package com.sg31.callalipayservice;

import com.sg31.alipayservice.OpenInterface;
import com.sg31.alipayservice.OpenInterface.Stub;

import android.support.v7.app.ActionBarActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends ActionBarActivity {

OpenInterface openInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

startRemoteService();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public void startRemoteService(){

Intent intent = new Intent();
intent.setAction("com.sg31.alipayservice");
intent.setPackage("com.sg31.alipayservice");
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 使用aidl中自动生成的方法来强转
openInterface = Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {
}
}, BIND_AUTO_CREATE);
}

public void callRemoteAlipayBtnClicked(View v){
//调用远程服务的支付方法
try {
openInterface.pay();
} catch (Exception e) {
e.printStackTrace();
}
}

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