use*_*949 4 service android react-native android-broadcastreceiver notification-listener
我正在使用React Native构建应用程序,我想使用Android服务NotificationListenerService.为了从服务中捕获数据,我需要一个广播接收器.如何在React Native Environment中设置BroadcastReceiver?
vin*_*ayr 10
我这样做的方式是emit使用事件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) {}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
.......
}
Run Code Online (Sandbox Code Playgroud)
请在这里介绍了有关发送事件的详细信息.
| 归档时间: |
|
| 查看次数: |
6689 次 |
| 最近记录: |