基本信息
源码名称:AIDLService 远程调用例子(AIDL)
源码大小:0.40M
文件格式:.zip
开发语言:Java
更新时间:2015-08-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
包括服务端与客户端两个工程文件
包括服务端与客户端两个工程文件
客户端代码:
package com.example.client; import com.example.server.IPerson; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button btn; private IPerson person; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("com.example.server.MyService"); bindService(intent, conn, Service.BIND_AUTO_CREATE); } }); } private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public synchronized void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub person = IPerson.Stub.asInterface(service); if(person != null){ try { String name1 = person.getValue(); Toast.makeText(MainActivity.this, "远程调用成功,值为:" name1, Toast.LENGTH_LONG).show(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(MainActivity.this, "远程调用失败", Toast.LENGTH_LONG).show(); } } } }; }
服务端代码
package com.example.server; import com.example.server.IPerson; import com.example.server.R; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private Button bindButton; private Button unbindButton; private IPerson iPerson; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindButton = (Button)findViewById(R.id.button1); unbindButton = (Button)findViewById(R.id.button2); bindButton.setOnClickListener(this); unbindButton.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: Intent intent = new Intent(MainActivity.this,MyService.class); bindService(intent, conn, Service.BIND_AUTO_CREATE); break; case R.id.button2: unbindService(conn); break; default: break; } } private ServiceConnection conn = new ServiceConnection() { //连接对象 @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub iPerson = IPerson.Stub.asInterface(service); if(iPerson!=null){ try { iPerson.setValue("AIDL TEST"); Toast.makeText(MainActivity.this, "赋值成功", Toast.LENGTH_LONG).show(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(MainActivity.this, "赋值失败", Toast.LENGTH_LONG).show(); } } } }; }