Skip to content

Commit

Permalink
function-MEITUAN-module:Android原生向JavaScript发送消息1:通过createNativeModul…
Browse files Browse the repository at this point in the history
…s创建HomeMessageEventModule用于原生端向服务端发送实时消息。注意:发送消息主要通过ReactContext的实例类完成,理论上能提高该实例的地方都具备通知JavaScript的功能

Signed-off-by: chenyabin1 <“[email protected]”>
  • Loading branch information
chenyabin1 committed Dec 11, 2019
1 parent 1741069 commit 8edaf1e
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 99 deletions.
1 change: 1 addition & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ dependencies {
// From node_modules
// implementation 'org.greenrobot:eventbus:3.1.1'
implementation files('src/main/libs/eventbus-3.1.1.jar')
compile 'org.jetbrains:annotations-java5:15.0'
}

// Run this once to be able to run the application with BUCK
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.common.react;


import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import org.jetbrains.annotations.Nullable;

import java.util.Map;

/**
* Created by Edwin,CHEN on 2019/12/11.
*/

public class HomeMessageEventModule extends ReactContextBaseJavaModule {

private static ReactApplicationContext reactApplicationContext;

private static HomeMessageEventModule homeMessageEventModule;

private String eventName;

private WritableMap params = Arguments.createMap();

public static HomeMessageEventModule newInstance() {
return new HomeMessageEventModule(reactApplicationContext);
}

public HomeMessageEventModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactApplicationContext = reactContext;
}

/**
* 发送本地时间给JS,相应的JavaScript代码要做好监听
* <p>
* WritableMap params = Arguments.createMap();
* params.putString("eventProperty", "someValue");
* String eventName = "EventReminder";
* sendEvent(reactContext, eventName, params);
* </p>
* @param eventName
* @param params
*/
public void sendEventToJs(String eventName,
@Nullable WritableMap params) {
if (reactApplicationContext != null) {
reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
}

public void sendEventToJs() {
if (reactApplicationContext != null) {
reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
}


public HomeMessageEventModule putEventName(String eventName) {
this.eventName = eventName;
return this;
}

public HomeMessageEventModule putParam(String key, Object value) {
if (key != null) {
if (value instanceof String) {
params.putString(key, (String) value);
} else if (value instanceof Integer) {
params.putInt(key,(Integer) value);
} else if (value instanceof Double || value instanceof Long) {
params.putDouble(key,(Double) value);
} else if (value instanceof WritableArray) {
params.putArray(key, (WritableArray) value);
} else if (value instanceof Boolean) {
params.putBoolean(key, (Boolean) value);
} else if (value instanceof Map) {
params.putMap(key, (WritableMap) value);
} else {
params.putNull(key);
}
}
return this;
}

@Override
public String getName() {
return HomeMessageEventModule.class.getSimpleName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
List<NativeModule> modules = new ArrayList<>();
modules.add(new ToastModule(reactContext));
modules.add(new HomeReactNativeMessageEventModule(reactContext, new HomeNativeMessageEventListener()));
// HomeMessageEventModule用于原生端向服务端发送实时消息
modules.add(new HomeMessageEventModule(reactContext));
return modules;
}

Expand Down
4 changes: 1 addition & 3 deletions android/app/src/main/java/com/common/react/ToastModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nullable;

/**
* Created by Edwin,CHEN on 2019/10/28.
*/
Expand Down Expand Up @@ -43,7 +41,7 @@ public String getName() {
* 让js来取变化的数据
* @return
*/
@Nullable

@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
Expand Down
196 changes: 101 additions & 95 deletions android/app/src/main/java/com/edwin/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import org.jetbrains.annotations.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.common.BaseEvent;
import com.common.Constants;
import com.common.HomeReactInstanceManager;
import com.common.react.HomeMessageEventModule;
import com.facebook.react.ReactRootView;
import com.meituan.MainActivity;
import com.meituan.R;
Expand All @@ -32,98 +32,104 @@

public class HomeFragment extends Fragment {

public static HomeFragment createHomeFragment() {
return new HomeFragment();
}

private View root;

private TextView eventBusFlagText;

private ReactRootView mReactRootView;

private Context mContext;

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (mContext == null) {
mContext = context;
}
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}

@SuppressLint("InflateParams")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.home_fragment, null);
root.findViewById(R.id.open_meituan_text_view).setOnClickListener(l);
root.findViewById(R.id.event_bus_sender_text_view).setOnClickListener(l);
eventBusFlagText = root.findViewById(R.id.event_bus_flag_text_view);
root.findViewById(R.id.event_bus_sender_text_view).setOnClickListener(l);
initReactRootView(root);
return root;
}

private void initReactRootView(View view) {
mReactRootView = new ReactRootView(mContext);

// The string here (e.g. "MyReactNativeApp") has to match
// the string in AppRegistry.registerComponent() in index.js
mReactRootView.startReactApplication(HomeReactInstanceManager.newInstance().getReactInstanceManager(), "MeiTuan", null);

LinearLayout homeReactRootView = view.findViewById(R.id.home_react_view);
if (homeReactRootView.indexOfChild(mReactRootView) == -1) {
(homeReactRootView).addView(mReactRootView,0);
}
}

private View.OnClickListener l = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.open_meituan_text_view:
startActivity(new Intent(getActivity(), MainActivity.class));
break;
case R.id.event_bus_sender_text_view:
EventBus.getDefault().post(new HomeEvent());
EventBus.getDefault().post(new HomeEvent(Constants.HOME_FRAGMENT_SENDER_REFRESH));
EventBus.getDefault().post(new HomeEvent(Constants.HOME_FRAGMENT_SENDER_COMPLEX, new HomeEvent()));
break;
}
}
};

@Subscribe(threadMode = ThreadMode.MAIN)
public void onReceiveRefreshEvent(BaseEvent baseEvent) {
switch (baseEvent.getEventType()) {
case Constants.HOME_FRAGMENT_SENDER_REFRESH:
Toast.makeText(getActivity(), "刷新就刷新", Toast.LENGTH_LONG).show();
break;
case Constants.HOME_FRAGMENT_SENDER_COMPLEX:
eventBusFlagText.setText("复杂点儿");
Toast.makeText(getActivity(), "复杂点儿", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getActivity(), "我就想验证下EventBus", Toast.LENGTH_LONG).show();
break;
}
}

@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);

if (mReactRootView != null) {
mReactRootView.unmountReactApplication();
}
}
public static HomeFragment createHomeFragment() {
return new HomeFragment();
}

private View root;

private TextView eventBusFlagText;

private ReactRootView mReactRootView;

private Context mContext;

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (mContext == null) {
mContext = context;
}
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}

@SuppressLint("InflateParams")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.home_fragment, null);
root.findViewById(R.id.open_meituan_text_view).setOnClickListener(l);
root.findViewById(R.id.event_bus_sender_text_view).setOnClickListener(l);
eventBusFlagText = root.findViewById(R.id.event_bus_flag_text_view);
root.findViewById(R.id.event_bus_sender_text_view).setOnClickListener(l);
root.findViewById(R.id.send_to_js_text_view).setOnClickListener(l);
initReactRootView(root);
return root;
}

private void initReactRootView(View view) {
mReactRootView = new ReactRootView(mContext);

// The string here (e.g. "MyReactNativeApp") has to match
// the string in AppRegistry.registerComponent() in index.js
mReactRootView.startReactApplication(HomeReactInstanceManager.newInstance().getReactInstanceManager(), "MeiTuan", null);

LinearLayout homeReactRootView = view.findViewById(R.id.home_react_view);
if (homeReactRootView.indexOfChild(mReactRootView) == -1) {
(homeReactRootView).addView(mReactRootView, 0);
}
}

private View.OnClickListener l = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.open_meituan_text_view:
startActivity(new Intent(getActivity(), MainActivity.class));
break;
case R.id.event_bus_sender_text_view:
EventBus.getDefault().post(new HomeEvent());
EventBus.getDefault().post(new HomeEvent(Constants.HOME_FRAGMENT_SENDER_REFRESH));
EventBus.getDefault().post(new HomeEvent(Constants.HOME_FRAGMENT_SENDER_COMPLEX, new HomeEvent()));
break;
case R.id.send_to_js_text_view:
// WritableMap params = Arguments.createMap();
// params.putString("eventProperty", "someValue");
// String eventName = "EventReminder";
HomeMessageEventModule.newInstance().putEventName("EventReminder").putParam("eventProperty", "someValue").sendEventToJs();
}
}
};

@Subscribe(threadMode = ThreadMode.MAIN)
public void onReceiveRefreshEvent(BaseEvent baseEvent) {
switch (baseEvent.getEventType()) {
case Constants.HOME_FRAGMENT_SENDER_REFRESH:
Toast.makeText(getActivity(), "刷新就刷新", Toast.LENGTH_LONG).show();
break;
case Constants.HOME_FRAGMENT_SENDER_COMPLEX:
eventBusFlagText.setText("复杂点儿");
Toast.makeText(getActivity(), "复杂点儿", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getActivity(), "我就想验证下EventBus", Toast.LENGTH_LONG).show();
break;
}
}

@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);

if (mReactRootView != null) {
mReactRootView.unmountReactApplication();
}
}

}
11 changes: 11 additions & 0 deletions android/app/src/main/res/layout/home_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@
android:text="EventBus标记"
android:textColor="@android:color/holo_blue_dark" />


<TextView
android:id="@+id/send_to_js_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="center"
android:padding="5dp"
android:text="Send To Js"
android:textColor="@android:color/holo_blue_dark" />

</LinearLayout>

<LinearLayout
Expand Down
10 changes: 9 additions & 1 deletion src/scene/Mine/MineScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


import React, {PureComponent} from 'react'
import { UIManager, findNodeHandle } from "react-native";
import { UIManager, findNodeHandle, NativeEventEmitter, NativeModules } from "react-native";
import ToastUtils from '../../utils/ToastUtils'

import {View, Text, StyleSheet, StatusBar, Image, TouchableOpacity, ScrollView, RefreshControl} from 'react-native'
Expand Down Expand Up @@ -62,6 +62,14 @@ class MineScene extends PureComponent<Props, State> {
}
}

componentDidMount() {
const eventEmitter = new NativeEventEmitter(NativeModules.ToastExample)
eventEmitter.addListener('EventReminder', (event) => {
// console.log(event.eventProperty) // "someValue"
alert(event.eventProperty)
})
}

onHeaderRefresh() {
this.setState({isRefreshing: true})

Expand Down

0 comments on commit 8edaf1e

Please sign in to comment.