因为我也在做物联网产品,所以这是我面临的最大问题之一。经过一些研究,我认为已经找到了解决这个问题的方法,或者你可以说是一个简单的技巧。
我已经在多个设备和多个版本中测试了这个技巧,并发现大多数设备都会响应。只有三星设备没有响应,还有一些华为设备和一些OPPO设备也没有响应。(我仍在寻找这些设备的解决方案)。
我注意到Android提供了一个访问通知的功能。您可以使用NotificationListenerService读取通知并执行一些操作。
它提供了一些重写方法:
onNotificationPosted()
onNotificationRemoved()
getActiveNotifications()
这里有一段代码:
创建一个扩展NotificationListenerService的服务。
class NLService extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
....
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
....
}
在AndroidManifest文件中,将此服务添加为:
<service
android:name=".NLService"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action
android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
这将允许您的应用程序读取收到的任何通知。
现在,这是主要代码:
在onNotificationPosted(StatusBarNotification sbn)中添加此代码:
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
try {
if (sbn.getNotification().actions != null) {
for (Notification.Action action : sbn.getNotification().actions)
{
Log.e(TAG, "" + action.title);
if (action.title.toString().equalsIgnoreCase("Answer")) {
Log.e(TAG, "" + true);
PendingIntent intent = action.actionIntent;
try {
intent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
就是这样!
一切都准备好了,运行应用程序,除了三星设备之外的任何设备,在显示来电通知时,将提供接听和拒绝/拒绝操作按钮,您可以使用它来接听电话。
要打开通知访问设置并允许您的应用程序读取通知,请使用:
Intent intent = new
Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intent);
只需为此创建一个POC,并让我知道它的工作原理。
如果这有帮助,请标记我的回答。
此外,如果您能提供关于三星设备的解决方案,请更新。
谢谢