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

本文介绍了一种通过创建Mailbox来接收来自PhoneNotifier进程的通知的方法,避免了注册监听Intent的传统方式。通过实例展示了如何在Android应用中实现这一机制。

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值