Android 上层实现IPC通讯

Android 上层实现IPC通讯 服务端,直接上代码:

package com.kz.server;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

public class Server extends Service {
	private static final String TAG = "Server";

	private static final int BIND = 1100;
	private static final int UNBIND = 1200;

	class IncomingHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			Log.i(TAG, "----------handleMessage() ");

			switch (msg.what) {
			case BIND:
				Message bindMessage = Message.obtain(null, BIND);
				try {
					msg.replyTo.send(bindMessage);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				break;
			case UNBIND:
				Message unBindMessage = Message.obtain(null, UNBIND);
				try {
					msg.replyTo.send(unBindMessage);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				break;
			case CommandList.SUBTITLE:
				String[] resultSubtitles = getSubtitles();
				Message subtitleMessage = Message.obtain(null, CommandList.SUBTITLE);
				Bundle subtitleBundle = new Bundle();
				subtitleBundle.putStringArray("resultSubtitles", resultSubtitles);
				subtitleMessage.setData(subtitleBundle);
				try {
					msg.replyTo.send(subtitleMessage);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				break;
			case CommandList.TIME_CURRENT:
				Integer resultTimeCurrent = getTimeCurrent();
				Message timeCurrentMessage = Message.obtain(null, CommandList.TIME_CURRENT);
				Bundle timeCurrentBuddle = new Bundle();
				timeCurrentBuddle.putInt("resultTimeCurrent", resultTimeCurrent);
				timeCurrentMessage.setData(timeCurrentBuddle);
				try {
					msg.replyTo.send(timeCurrentMessage);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				break;
			case CommandList.TIME_TOTAL:
				Integer resultTimeTotal = getTimeTotal();
				Message timeTotalMessage = Message.obtain(null, CommandList.TIME_TOTAL);
				Bundle timeTotalBuddle = new Bundle();
				timeTotalBuddle.putInt("resultTimeTotal", resultTimeTotal);
				timeTotalMessage.setData(timeTotalBuddle);
				try {
					msg.replyTo.send(timeTotalMessage);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				break;
			case CommandList.LANGUAGE:
				String[] resultLanguages = getLanguages();
				Message languageMessage = Message.obtain(null, CommandList.LANGUAGE);
				Bundle languageBundle = new Bundle();
				languageBundle.putStringArray("resultLanguages", resultLanguages);
				languageMessage.setData(languageBundle);
				try {
					msg.replyTo.send(languageMessage);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				break;
			default:
				super.handleMessage(msg);
			}

		}

	}

	Messenger mMessenger = new Messenger(new IncomingHandler());

	@Override
	public void onCreate() {
		Log.i(TAG, "----------onCreate() ");

		super.onCreate();
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.i(TAG, "----------onBind() ");

		return mMessenger.getBinder();
	}

	@Override
	public void onDestroy() {
		Log.i(TAG, "----------onDestroy() ");

		super.onDestroy();
	}

	private String[] getSubtitles() {
		Log.i(TAG, "----------getSubtitles() ");

		return new String[] { "中文字幕", "英文字幕", "泰文字幕" };
	}

	private String[] getLanguages() {
		Log.i(TAG, "----------getLanguages() ");

		return new String[] { "汉语", "英语", "泰语" };
	}

	private Integer getTimeTotal() {
		Log.i(TAG, "----------getTimeTotal() ");

		return 150000;
	}

	private Integer getTimeCurrent() {
		Log.i(TAG, "----------getTimeCurrent() ");

		return 132000;
	}

}

Android 上层实现IPC通讯 服务端 代码:

package com.kz.client;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ClientActivity extends Activity implements ChangeInfoCallback {

	private boolean isBind = false;
	private TextView txt, txtsubtitle, txtlanguage;
	private LinearLayout layout;
	private Button bind, unbind, subtitle, language;

	private ManageChangeInfo manageInfo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		manageInfo = new ManageChangeInfo();
		manageInfo.registerCallback(this);

		txt = (TextView) findViewById(R.id.txt);
		txtsubtitle = (TextView) findViewById(R.id.txtsubtitle);
		txtlanguage = (TextView) findViewById(R.id.txtlanguage);

		bind = (Button) findViewById(R.id.bindBtn);
		unbind = (Button) findViewById(R.id.unbindBtn);
		subtitle = (Button) findViewById(R.id.subtitle);
		language = (Button) findViewById(R.id.language);

		layout = (LinearLayout) findViewById(R.id.layout);

		bind.setOnClickListener(click);
		unbind.setOnClickListener(click);
		subtitle.setOnClickListener(click);
		language.setOnClickListener(click);
		layout.setOnClickListener(click);
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (isBind) {
			unbindService(conn);
			isBind = false;
		}
	}

	private View.OnClickListener click = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			int id = v.getId();
			switch (id) {
			case R.id.bindBtn:
				Intent service = new Intent("com.kz.dvd");
				bindService(service, conn, BIND_AUTO_CREATE);
				break;
			case R.id.unbindBtn:
				if (isBind && mService != null) {
					unbindService(conn);
					Message msg = Message.obtain(null, UNBIND);
					msg.replyTo = mMessenger;
					try {
						mService.send(msg);
					} catch (RemoteException e) {
						e.printStackTrace();
					}
					isBind = false;
				}
				break;
			case R.id.subtitle:
				sendMessage(CommandList.SUBTITLE);
				break;
			case R.id.language:
				sendMessage(CommandList.LANGUAGE);
				break;
			}
		}
	};

	private void sendMessage(int action) {
		if (isBind && mService != null) {
			Message msg = Message.obtain(null, action);
			msg.replyTo = mMessenger;
			try {
				mService.send(msg);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	private static final int BIND = 1100;
	private static final int UNBIND = 1200;

	private class IncomingHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case BIND:
				layout.setVisibility(View.VISIBLE);
				txt.setText("bind");
				break;
			case UNBIND:
				layout.setVisibility(View.INVISIBLE);
				txt.setText("unbind");
				txtsubtitle.setText("字幕-->");
				txtlanguage.setText("语言-->");
				break;
			case CommandList.SUBTITLE:
				String[] resultSubtitles = msg.getData().getStringArray("resultSubtitles");
				manageInfo.onChangeSubtitle(getStringFromArray(resultSubtitles));
				break;
			case CommandList.LANGUAGE:
				String[] resultLanguages = msg.getData().getStringArray("resultLanguages");
				manageInfo.onChangeLanguage(getStringFromArray(resultLanguages));
				break;
			default:
				super.handleMessage(msg);
			}
		}
	};

	private Messenger mMessenger = new Messenger(new IncomingHandler());
	private Messenger mService;

	private ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			isBind = true;
			mService = new Messenger(service);
			Message msg = Message.obtain(null, BIND);
			msg.replyTo = mMessenger;
			try {
				mService.send(msg);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mService = null;
		}

	};

	@Override
	public void changeSubtitle(String subtitle) {
		txtsubtitle.setText("字幕-->" + subtitle);
	}

	@Override
	public void changeLanguage(String language) {
		txtlanguage.setText("语言-->" + language);
	}
	
	private String getStringFromArray(String[] strs) {
		StringBuilder sb = new StringBuilder();
		sb.append('[');
		for (String str : strs) {
			sb.append(str).append(",");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(']');
		return sb.toString();
	}

}
主程为上面两个类,部分接口没公布,参考资料为官网的services主题。

你可能感兴趣的:(Android 上层实现IPC通讯)