好记性不如铅笔头

android, 编程

Android应用开发笔记:AIDL

最近有个需求,需要使用AIDL进行通讯。下面简单的备忘下AIDL的使用方式。

github:

https://github.com/cstriker1407/android/tree/master/HelloAIDL 】

CONTENTS

1)新建一个.aidl的文件,用来声明通讯接口。

package net.yeah.cstriker1407;
interface IServicePlayer{
	void play();
	void seekTo(int current);
	boolean setLoop(boolean loop);
}

 需要注意的是:

package的声明需要和.aidl的位置一致。

此时如果.aidl建立OK,在工程的gen目录下会生成一个java文件:IServicePlayer.java

2)编写service,实现服务端的接口逻辑。

public class MusicService extends Service

{
	IServicePlayer.Stub stub = new IServicePlayer.Stub()
	{
		@Override
		public boolean setLoop(boolean loop) throws RemoteException
		{
			Log.d("", "setLoop" + Boolean.toString(loop));
			return false;
		}

		@Override
		public void seekTo(int current) throws RemoteException
		{
			Log.d("", "" + "seekTo" + current);
		}

		@Override
		public void play() throws RemoteException
		{
			Log.d("", "" + "play");
		}
	};

	@Override
	public IBinder onBind(Intent arg0)
	{
		return stub;
	}
}

 3)编写客户端的调用代码。

在本demo中,客户端和服务器放在一起了。在其他的应用中可以分成两个不同的工程,通过保证.aidl的唯一性来进行通讯。

public class MainActivity extends Activity {

	 IServicePlayer iPlayer;

	private ServiceConnection conn = new ServiceConnection()
	{
		public void onServiceConnected(ComponentName className, IBinder service)
		{
			iPlayer = IServicePlayer.Stub.asInterface(service);
		}

		public void onServiceDisconnected(ComponentName className)
		{
			iPlayer = null;
		};
	};

	protected void onDestroy()
	{
		super.onDestroy();
		unbindService(conn);  
	}




	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        bindService(new Intent(MainActivity.this, MusicService.class), conn, Context.BIND_AUTO_CREATE);
        startService(new Intent(MainActivity.this, MusicService.class));
        
        ((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				try
				{
					iPlayer.play();
					iPlayer.setLoop(true);
					iPlayer.seekTo(100);
				}
				catch (RemoteException e)
				{
					e.printStackTrace();
				}
			}
		});
    }
}

 备注:

1)本demo的部分代码来自【 http://android.yaohuiji.com/archives/728 】。

2)更复杂的aidl实例没有编写,后续可能会补上。

3)关于service的进程问题,可以参考下android的开发文档,【 http://developer.android.com/guide/topics/manifest/service-element.html 】,里面的android:process的介绍会有很大的帮助。

android:process
The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package. The <application> element's process attribute can set a different default for all components. But component can override the default with its own process attribute, allowing you to spread your application across multiple processes.
If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

 

发表评论

14 + 5 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据