博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android绑定远程服务
阅读量:7243 次
发布时间:2019-06-29

本文共 2024 字,大约阅读时间需要 6 分钟。

hot3.png

方法:

服务项目目录结构:
调用方项目目录结构:
Iservice.aidl文件 android interface definition language 安卓用于项目间使用同一接口的方式:

package com.pas.payservice;interface IService{ 	void callpay();}

全为public所以不可以加修饰符, 调用方需要拷贝此文件及包结构

Service核心:

public IBinder onBind(Intent arg0)	{		Log.i(tag, "远程支付服务:onBind");		return new MyBinder();	}	private class MyBinder extends IService.Stub	{		@Override		public void callpay()		{			pay();		}			}

 

调用方主AC:注意与调用本地服务的api区别

package com.pas.useage;import com.pas.payservice.IService;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.view.Menu;import android.view.View;public class MainActivity extends Activity{	private Intent intent;	private IService binder;	private MyConn conn;	@Override	protected void onCreate(Bundle savedInstanceState)	{		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		intent = new Intent();		intent.setAction("com.pas.alipay");	}	@Override	public boolean onCreateOptionsMenu(Menu menu)	{		// Inflate the menu; this adds items to the action bar if it is present.		getMenuInflater().inflate(R.menu.main, menu);		return true;	}	public void bind(View v)	{		conn = new MyConn();		startService(intent);		bindService(intent, conn, BIND_AUTO_CREATE);	}	private class MyConn implements ServiceConnection	{		@Override		public void onServiceConnected(ComponentName name, IBinder service)		{			binder = IService.Stub.asInterface(service);		}		@Override		public void onServiceDisconnected(ComponentName name)		{			// TODO Auto-generated method stub		}	}	public void call(View v)	{		try		{			binder.callpay();		} catch (RemoteException e)		{			e.printStackTrace();		}	}	@Override	protected void onDestroy()	{		try		{			if (conn != null)			{				unbindService(conn);			}		} catch (Exception e)		{			// TODO: handle exception		}				super.onDestroy();	}}

转载于:https://my.oschina.net/u/1246663/blog/199718

你可能感兴趣的文章
No repository found at http://m2eclipse.sonatype.org/sites/m2e
查看>>
如何提高网速20%!
查看>>
我的友情链接
查看>>
apache mod_expires缓存静态文件
查看>>
加密、解密的原理及Openssl创建CA和ssh的基础应用
查看>>
我的友情链接
查看>>
O2O的另类演绎
查看>>
cool1 dual生态手机的产品经:基于体验基础的商业模式变革
查看>>
安卓滑动隐藏显示菜单功能实现(还有蒙板校果),只显示一半view
查看>>
使用URL在本地显示网络上的图片
查看>>
免费公测:RDS只读实例
查看>>
阿里云 Aliplayer高级功能介绍(二):缩略图
查看>>
设计原则解读——单一职责原则、里氏替换原则
查看>>
IE10的补丁标题显示9.0!
查看>>
每天一个linux命令(15):tail 命令
查看>>
co yield避免嵌套详细代码示例。
查看>>
基于两种创建docker镜像的启动容器时区别总结
查看>>
jdk1.6的集合源码阅读之ArrayList
查看>>
Android应用实例之---使用Linkify + 正则式区分微博文本链接及跳转处理
查看>>
关于uploadify使用的一些小问题
查看>>