标签:
前言第四步:你可以使用service进行方法调用,传输数据等。
一、 AIDL介绍
在Android中,每个应用(Application)执行在它自己的进程中,无法直接调用到其他应用的资源,这也符合“沙箱”的理念。所谓沙箱原理,一般来说用在移动电话业务中,简单地说旨在部分地或全部地隔离应用程序。关于沙箱技术我们这里就不多做介绍了,可以参看51CTO的这篇文章。<span style="font-family:Microsoft YaHei;font-size:18px;">package com.android.hellosumaidl; // Interface declaration interface IAdditionService { // You can pass the value of in, out or inout // The primitive types (int, boolean, etc) are only passed by in int add(in int value1, in int value2); }</span>一旦文件被保存,Android的AIDL工具会在gen/com/android/hellosumaidl这个文件夹里自动生成对应的
<span style="font-family:Microsoft YaHei;font-size:14px;">package com.android.hellosumaidl; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; /* * This class exposes the service to client */ public class AdditionService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { return new IAdditionService.Stub() { /* * Implement com.android.hellosumaidl.IAdditionService.add(int, int) */ @Override public int add(int value1, int value2) throws RemoteException { return value1 + value2; } }; } @Override public void onDestroy() { super.onDestroy(); }</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">/* * This inner class is used to connect to the service */ class AdditionServiceConnection implements ServiceConnection { public void onServiceConnected(ComponentName name, IBinder boundService) { service = IAdditionService.Stub.asInterface((IBinder)boundService); Toast.makeText(HelloSumAidlActivity.this, "Service connected", Toast.LENGTH_LONG).show(); }</span><pre name="code" class="java"><span style="font-family: 'Microsoft YaHei'; line-height: 1.6em;"> </span>public void onServiceDisconnected(ComponentName name) { service = null; Toast.makeText(HelloSumAidlActivity.this, "Service disconnected", Toast.LENGTH_LONG).show(); }}
<span style="font-family:Microsoft YaHei;font-size:14px;">package com.android.hellosumaidl; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; public class HelloSumAidlActivity extends Activity { IAdditionService service; AdditionServiceConnection connection; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initService(); Button buttonCalc = (Button)findViewById(R.id.buttonCalc); buttonCalc.setOnClickListener(new OnClickListener() { TextView result = (TextView)findViewById(R.id.result); EditText value1 = (EditText)findViewById(R.id.value1); EditText value2 = (EditText)findViewById(R.id.value2); @Override public void onClick(View v) { int v1, v2, res = -1; v1 = Integer.parseInt(value1.getText().toString()); v2 = Integer.parseInt(value2.getText().toString()); try { res = service.add(v1, v2); } catch (RemoteException e) { e.printStackTrace(); } result.setText(Integer.valueOf(res).toString()); } }); } @Override protected void onDestroy() { super.onDestroy(); releaseService(); } /* * This inner class is used to connect to the service */ class AdditionServiceConnection implements ServiceConnection { public void onServiceConnected(ComponentName name, IBinder boundService) { service = IAdditionService.Stub.asInterface((IBinder)boundService); Toast.makeText(HelloSumAidlActivity.this, "Service connected", Toast.LENGTH_LONG).show(); } public void onServiceDisconnected(ComponentName name) { service = null; Toast.makeText(HelloSumAidlActivity.this, "Service disconnected", Toast.LENGTH_LONG).show(); } } /* * This function connects the Activity to the service */ private void initService() { connection = new AdditionServiceConnection(); Intent i = new Intent(); i.setClassName("com.android.hellosumaidl", com.android.hellosumaidl.AdditionService.class.getName()); boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE); } /* * This function disconnects the Activity from the service */ private void releaseService() { unbindService(connection); connection = null; } }</span>
Fig 1 : 填写数字前
Fig 2 : 按下计算按钮后
标签:
原文地址:http://blog.csdn.net/baidu_34934230/article/details/51363857