When i was looking at the strace output closely. I noticed a service named android.telephony.PhoneNotifier. It stuck somewhere in my head. A few(?) days later i ran info the android.os.Mailbox class. Another few days later, when poking into the dexdump output of Phone.apk, it all came together. Here’s the result of experimentation. Basically you can add use a mailbox to receive the notifications from the process that controls the phone. It’s an alternative to registering and listening for intents.
We basically create a mailbox and send a message to the PhoneNotifier to send messages to that mailbox
package org.apache.android.ril;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Mailbox;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.HashMap;
public class RILSandbox extends ListActivity {
MyHandler handler = new MyHandler();
Mailbox myMB = Mailbox.createAnonymous(handler);
ArrayList items = new ArrayList();
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
registerForNotification(1);
registerForNotification(2);
registerForNotification(3);
registerForNotification(4);
registerForNotification(5);
registerForNotification(6);
registerForNotification(7);
}
private void registerForNotification(int type) {
Message msg = Message.obtain();
msg.what = type;
HashMap map = new HashMap();
map.put("mailbox", myMB);
msg.setData(map);
Mailbox.sendToPublished("android.telephony.PhoneNotifier", msg, null);
}
public class MyHandler extends Handler implements Mailbox.Receiver {
public Object onNewMail(Message message, Mailbox.Completion completion) {
switch (message.what) {
case 1:
items.add("PhoneState: " + message.getData());
break;
case 2:
items.add("ServiceState: " + message.getData());
break;
case 3:
items.add("SignalStrength - 1: " + message.getData());
items.add("SignalStrength - 2: " + message.arg1);
break;
case 4:
items.add("DataConnection: " + message.getData());
break;
case 5:
items.add("MessageWaitingChanged - 1: " + message.getData());
items.add("MessageWaitingChanged - 2: " + message.arg1);
break;
case 6:
items.add("CallForwardingChanged - 1: " + message.getData());
items.add("CallForwardingChanged - 2: " + message.arg1);
break;
default:
items.add("Unknown - 1: " + message.what);
items.add("Unknown - 2: " + message.getData());
break;
}
updateListAdapter();
return null;
}
}
private void updateListAdapter() {
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, items));
}
}
When the app starts up, you will see a few messages float by
Try “gsm call +223424″ after telnet-ing to 5554 to see additional notifications.
Download Source and APK from here – http://people.apache.org/~dims/android/RILSandbox.zip
本文介绍了一种通过创建Mailbox来接收来自PhoneNotifier进程的通知的方法,避免了注册监听Intent的传统方式。通过实例展示了如何在Android应用中实现这一机制。
1139

被折叠的 条评论
为什么被折叠?



