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

<think> 我们正在讨论的是在Unity中使用`using UnityEngine.Android.Notifications;`命名空间时出现报错的问题。 根据之前的引用和Unity版本的变化,我们知道Unity在2019.3版本之后引入了新的通知系统,并且将通知相关的API移动到了新的命名空间。 引用[2]中展示的代码使用的是: ```csharp #if UNITY_ANDROID using Unity.Notifications.Android; #endif ``` 而用户尝试使用的`using UnityEngine.Android.Notifications;`可能是旧版本或者错误的方式。 因此,报错的原因可能是: 1. 使用的Unity版本较低(低于2019.3),没有这个命名空间。 2. 使用的Unity版本符合,但未导入Mobile Notifications包。 3. 命名空间引用错误,正确的应该是`Unity.Notifications.Android`。 解决方案: 1. 确认Unity版本:需要2019.3或更高版本,推荐2020.1及以上。 2. 安装Mobile Notifications包: 打开Package Manager -> 搜索"Mobile Notifications"并安装(可能需要启用Preview Packages)。 3. 修改命名空间引用: 将`using UnityEngine.Android.Notifications;`改为`using Unity.Notifications.Android;` 如果用户使用的是旧版本Unity(如2018.x),则无法使用这个包,需要改用其他方式,比如: - 使用UnityEngine.iOS.LocalNotification(仅iOS)和AndroidJavaObject调用原生Android通知API(复杂)。 - 使用第三方插件。 但是,根据引用[4]中提到的`LocalNotification.SendNotification`,这可能是某个第三方插件的API(并非Unity官方),所以需要确认是否使用了第三方插件。 因此,我们按照以下步骤解决: 步骤1:检查Unity版本 步骤2:安装Mobile Notifications包 步骤3:修改代码中的命名空间 示例代码(引用[2]中的写法): ```csharp #if UNITY_ANDROID using Unity.Notifications.Android; #endif ``` 然后在代码中使用`AndroidNotificationCenter`等类。 注意:如果项目同时需要支持iOS,可以使用条件编译,因为iOS的通知命名空间是`UnityEngine.iOS`(引用[3])。 如果用户已经按照上述步骤操作仍然报错,请检查包是否成功导入,并查看控制台错误信息。 如果错误是关于权限的,请参考引用[1]动态申请权限。 下面给出一个完整的解决方案: 1. 安装Mobile Notifications包(Window -> Package Manager -> 选择Unity Registry -> 搜索Mobile Notifications -> 安装)。 2. 修改代码: ```csharp // 移除错误的命名空间引用 // 替换为 #if UNITY_ANDROID using Unity.Notifications.Android; #endif ``` 3. 在需要的地方使用Android通知API(如创建通道、发送通知)。 4. 对于Android,确保在AndroidManifest.xml中添加了通知权限(如果使用2019.3+的Mobile Notifications包,它可能会自动添加,但最好检查一下)。 5. 如果项目中同时有iOS通知,可以这样写: ```csharp #if UNITY_ANDROID using Unity.Notifications.Android; #elif UNITY_IOS using UnityEngine.iOS; #endif ``` 6. 如果用户使用的Unity版本过低(如2018.x),则建议升级Unity版本,或者使用第三方通知插件(如OneSignal, Firebase等)。 综上所述,报错的原因主要是命名空间的改变以及包未正确安装。 下面我们根据用户的问题,总结回答: </think> ### `using UnityEngine.Android.Notifications;` 报错的原因及解决方法 在Unity中使用 `using UnityEngine.Android.Notifications;` 报错的主要原因如下: #### 1. **命名空间路径错误**(最常见原因)[^2][^4] Unity官方通知系统的正确命名空间是: ```csharp using Unity.Notifications.Android; // 正确命名空间 ``` 而非: ```csharp using UnityEngine.Android.Notifications; // 错误路径 ``` #### 2. **未安装Mobile Notifications包** - Unity 2019.3+ 将通知功能移到了独立包 - 解决方案: 1. 打开 **Window > Package Manager** 2. 搜索并安装 **Mobile Notifications** 包 3. 确保在Android平台勾选安装(iOS需要单独配置) #### 3. **Unity版本兼容性问题** - 低于 **Unity 2019.3**:没有内置通知系统,需用第三方方案 - Unity 2019.3-2020.1:实验性通知API,路径不统一 - Unity 2020.1+:稳定API,使用 `Unity.Notifications` 命名空间 #### 4. **平台编译条件缺失** 未添加平台预处理指令会导致iOS编译错误: ```csharp // 正确做法:添加平台条件编译 #if UNITY_ANDROID using Unity.Notifications.Android; #elif UNITY_IOS using UnityEngine.iOS; #endif ``` --- ### 完整解决方案 1. **修改命名空间** ```csharp // 替换错误路径 using Unity.Notifications.Android; ``` 2. **安装通知包** ```csharp // 包安装后代码示例 AndroidNotificationChannel channel = new() { Id = "channel_id", Name = "每日提醒", Importance = Importance.High }; AndroidNotificationCenter.RegisterNotificationChannel(channel); ``` 3. **版本适配方案** - Unity < 2019.3:使用第三方插件(如Firebase, OneSignal) - Unity 2019.3+:启用 **Edit > Project Settings > Player > Other Settings > Active Input Handling** 为 **Both** 4. **权限处理**(Android 13+必需) ```csharp // 动态请求权限[^1] if (!Permission.HasUserAuthorizedPermission("android.permission.POST_NOTIFICATIONS")) { Permission.RequestUserPermission("android.permission.POST_NOTIFICATIONS"); } ``` > **验证步骤**:安装Mobile Notifications包后,尝试输入`AndroidNotification` - 如有自动补全即配置成功。 --- ### 平台差异注意事项 1. **iOS特殊处理**(引用[3]) ```csharp #if UNITY_IOS // iOS使用不同API[^3] UnityEngine.iOS.LocalNotification notif = new() { fireDate = DateTime.Now.AddHours(1), alertBody = "iOS通知内容" }; NotificationServices.ScheduleLocalNotification(notif); #endif ``` 2. **Android Manifest配置** ```xml <!-- 必须添加权限声明 --> <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> ``` --- ### 备选方案(旧版Unity) 如果无法升级Unity,可使用兼容层: ```csharp // 兼容层示例 public static class NotificationWrapper { public static void SendNotification(string title, string msg) { #if UNITY_ANDROID && !UNITY_EDITOR // Android原生API调用 using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) using (var context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) { var notificationClass = new AndroidJavaClass("android.app.Notification"); // ... 原生实现逻辑 } #endif } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值