连接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:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.programload"
android:sharedUserId="android.uid.system"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
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; }