在React Native上设置Android BroadcastReceiver
发布时间:2020-12-15 09:32:17 所属栏目:百科 来源:网络整理
导读:我正在使用React Native构建应用程序,我想使用 Android服务NotificationListenerService.为了从服务中捕获数据,我需要一个广播接收器.如何在React Native Environment中设置BroadcastReceiver? 解决方法 我这样做的方法是使用getJSModule发出事件 MyListene
我正在使用React Native构建应用程序,我想使用
Android服务NotificationListenerService.为了从服务中捕获数据,我需要一个广播接收器.如何在React Native Environment中设置BroadcastReceiver?
解决方法
我这样做的方法是使用getJSModule发出事件
MyListener.java public class MyListener extends NotificationListenerService { @Override public void onNotificationPosted(StatusBarNotification sbn) { if (sbn.getNotification().tickerText == null) { return; } WritableNativeMap params = new WritableNativeMap(); params.putString("tickerText",sbn.getNotification().tickerText.toString()); params.putString("packageName",sbn.getPackageName()); MyModule.sendEvent("notificationReceived",params); } @Override public void onNotificationRemoved(StatusBarNotification sbn) {} } MyModule.java public class MyModule extends ReactContextBaseJavaModule implements ActivityEventListener { private static ReactApplicationContext reactContext; public MyModule(ReactApplicationContext reactContext) { super(reactContext); this.reactContext = reactContext; reactContext.addActivityEventListener(this); } public static void sendEvent(String event,WritableNativeMap params) { reactContext .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit(event,params); } ....... } 检查here以获取有关发送事件的更多详细信息 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |