Android – Listen to Phone Notifications using android.os.Mailbox

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 _disibledevent=> 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
Tags: 

延伸阅读

最新评论

发表评论