Android - Listen to Phone Notifications using android.os.Mailbox
Here is a screen shot.

Notes
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));
}
}
Notes
- 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 - RILSandbox.zip
About this entry
You’re currently reading “Android - Listen to Phone Notifications using android.os.Mailbox,” an entry on Show me the code! - By Davanum Srinivas
- Published:
- 12.27.07 / 10am
- Category:
- Uncategorized
- Tags:
1 Comment
Jump to comment form | comments rss [?] | trackback uri [?]