package com.example.aidl.calculate;
interface CalculateInterface {
double doCalculate(double a, double b);
}
package com.example.aidl.calculate;
interface CalculateInterface {
double doCalculate(double a, double b);
}
package com.example.calculate;
import com.example.aidl.calculate.CalculateInterface;
import com.example.aidl.calculate.CalculateInterface.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class CalculateService extends Service {
private static final String TAG = "CalculateService";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
logE("onBind()");
return mBinder;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
logE("onCreate()");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
logE("onStart()");
super.onStart(intent, startId);
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
logE("onUnbind()");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
logE("onDestroy()");
super.onDestroy();
}
private static void logE(String str) {
Log.e(TAG, "--------" + str + "--------");
}
private final CalculateInterface.Stub mBinder = new CalculateInterface.Stub() {
@Override
public double doCalculate(double a, double b) throws RemoteException {
// TODO Auto-generated method stub
Log.e("Calculate", "远程计算中");
Calculate calculate = new Calculate();
double answer = calculate.calculateSum(a, b);
return answer;
}
};
}
package com.example.calculate;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.aidl.calculate.CalculateInterface;
import com.example.aidlcalculatedemoclient.R;
public class CalculateClient extends Activity {
private static final String TAG = "CalculateClient";
private Button btnCalculate;
private EditText etNum1;
private EditText etNum2;
private TextView tvResult;
private CalculateInterface mService;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
logE("disconnect service");
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
logE("connect service");
mService = CalculateInterface.Stub.asInterface(service);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle args = new Bundle();
Intent intent = new Intent("com.example.calculate.CalculateService");
intent.putExtras(args);
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
etNum1 = (EditText) findViewById(R.id.et_num_one);
etNum2 = (EditText) findViewById(R.id.et_num_two);
tvResult = (TextView) findViewById(R.id.tv_result);
btnCalculate = (Button) findViewById(R.id.btn_cal);
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
logE("开始远程运算");
try {
double num1 = Double.parseDouble(etNum1.getText().toString());
double num2 = Double.parseDouble(etNum2.getText().toString());
String answer = "计算结果:" + mService.doCalculate(num1, num2);
tvResult.setTextColor(Color.BLUE);
tvResult.setText(answer);
} catch (RemoteException e) {
}
}
});
}
private void logE(String str) {
Log.e(TAG, "--------" + str + "--------");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aidlcaculatedemoserver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.aidlcaculatedemoserver.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="com.example.calculate.CalculateService">
<intent-filter>
<action android:name="com.example.calculate.CalculateService" />
</intent-filter>
</service>
</application>
</manifest>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有