android 應用層如何鏈接rild

鏈接rild的方式,一種是經過調用framework接口來實現,經過相似PhoneFactory.getDefaultPhone()方式,固然若是要發送自定義的指令,還要增長相應的接口指令,比較麻煩,研究後再補上相關信息

還有別一種方式,就是直接經過socket鏈接,用有名socket進行鏈接。好比"rild"這個,若是直接鏈接,也許資源已佔沒法鏈接傳遞信息。但若是鏈接「rild-debug」則能夠順利鏈接。

告別要注意的是:
AndroidManifest.xml  添加android:sharedUserId="android.uid.system"
activity中要添加  android:process="com.android.phone" 使它在進程com.android.phone中
這樣纔有權限編譯運行,固然要在源碼下編譯才能夠,要否則要提示 has no signatures that match those in shared user android.uid.system這樣的信息。
解釋一下原理,首先加入android:sharedUserId="android.uid.system"這個屬性。經過Shared User id,擁有同一個User id的多個APK能夠配置成運行在同一個進程中。那麼把程序的UID配成android.uid.system,也就是要讓程序運行在系統進程中,這樣就有權限鏈接socket了
code: java

<?xml version="1.0" encoding="utf-8"?> android

<manifest xmlns:android="http://schemas.android.com/apk/res/android" app

        package="com.android.programload" socket

        android:sharedUserId="android.uid.system" ui

        android:versionCode="1" spa

        android:versionName="1.0" > debug

        <uses-sdk android:minSdkVersion="8" /> code

        <application orm

                android:icon="@drawable/ic_launcher" xml

                android:label="@string/app_name" >

                <activity

                        android:label="@string/app_name"  android:process="com.android.phone"

                        android:name=".ProgramLoad" >

                        <intent-filter >

                                <action android:name="android.intent.action.MAIN" />

                                <category android:name="android.intent.category.LAUNCHER" />

                        </intent-filter>

                </activity>

        </application>

        <uses-permission android:name="android.permission.INTERNET" />

        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

</manifest>
關鍵代碼以下:

public void load(String data)
	{
		int number = 1;
		String senddata = data;
		LocalSocket s= new LocalSocket();
		LocalSocketAddress l = new LocalSocketAddress("rild-debug", LocalSocketAddress.Namespace.RESERVED);
		try {
			s.connect(l);

		} catch (IOException e) {
			e.printStackTrace();
			try {
				if (s != null) {
					s.close();
				}
			} catch (IOException ex2) {
			}
		}
		mSocket = s;
		SendData(number, senddata);
	}
	public void CloseSocket()
	{   
		try {
			if(mSocket != null)
				mSocket.close();
		} catch (IOException ex) {
		}   

		mSocket = null;
	} 
	public void SendData(int number, String senddata)
	{
		byte[] numLength = new byte[4];
		byte[] dataLength = new byte[4];
		byte[] data = senddata.getBytes();
		try {
			//InputStream inputStream=s.getInputStream();
			//OutputStream OutputStream=s.getOutputStream();

			Log.e(LOG_TAG, "send number ="+ number);
			numLength[3] = numLength[2] = 0;
			numLength[1] = (byte)((number >> 8) & 0xff);
			numLength[0] = (byte)((number) & 0xff);
			mSocket.getOutputStream().write(numLength);

			Log.e(LOG_TAG, "send datalen ="+ data.length);
			dataLength[3] = dataLength[2] = 0;
			dataLength[1] = (byte)((data.length >> 8) & 0xff);
			dataLength[0] = (byte)((data.length) & 0xff);
			mSocket.getOutputStream().write(dataLength);

			Log.e(LOG_TAG, "send data ="+ senddata);
			mSocket.getOutputStream().write(data);
		} catch (IOException e) {
			//TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public void ReceiverData()
	{
		int length = 0;
		byte[] buffer =  new byte[RIL_MAX_COMMAND_BYTES];;
		try{
			InputStream is = mSocket.getInputStream();
			for(;;)
			{
				length = readRilMessage(is, buffer);
				if (length < 0) {
					//End-of-stream reached
					break;
				}

			}

			Log.e(LOG_TAG, "socket reading message len "+ length);
			if(length > 0)
			{
				Log.e(LOG_TAG, "socket reading message "+ buffer);
			}
		} catch (java.io.IOException ex) {
		} catch (Throwable tr) {
		}
	}
	private static int readRilMessage(InputStream is, byte[] buffer)
		throws IOException {
		int countRead;
		int offset;
		int remaining;
		int messageLength;

		offset = 0;
		remaining = 4;
		do {
			countRead = is.read(buffer, offset, remaining);

			if (countRead < 0 ) { 
				Log.e(LOG_TAG, "socket reading message length error");
				return -1; 
			}   

			offset += countRead;
			remaining -= countRead;
		} while (remaining > 0); 
		Log.e(LOG_TAG, "socket reading message buffer ="+buffer);

		offset = 0;
		remaining = 4;
		do {
			countRead = is.read(buffer, offset, remaining);

			if (countRead < 0 ) {
				Log.e(LOG_TAG, "socket reading message length error");
				return -1;
			}

			offset += countRead;
			remaining -= countRead;
		} while (remaining > 0);
		Log.e(LOG_TAG, "socket reading message1 buffer ="+buffer);

		messageLength = ((buffer[3] & 0xff) << 24)
			| ((buffer[2] & 0xff) << 16)
			| ((buffer[1] & 0xff) << 8)
			| (buffer[0] & 0xff);
		 
		Log.e(LOG_TAG, "socket reading message1 buffer messageLength ="+ messageLength);
		offset = 0;
		remaining = messageLength;
		do {
			countRead = is.read(buffer, offset, remaining);

			if (countRead < 0 ) {
				Log.e(LOG_TAG, "socket reading message error.  messageLength=" + messageLength
						+ " remaining=" + remaining);
				return -1;
			}

			offset += countRead;
			remaining -= countRead;
		} while (remaining > 0);
		Log.e(LOG_TAG, "socket reading message2 buffer ="+buffer);

		return messageLength;
	}
相關文章
相關標籤/搜索