Javascript 从widget-android触发react本机事件
我已经使用react native制作了一个应用程序,但是我需要从android小部件触发react事件。这是我当前的设置:Javascript 从widget-android触发react本机事件,javascript,android,events,react-native,native,Javascript,Android,Events,React Native,Native,我已经使用react native制作了一个应用程序,但是我需要从android小部件触发react事件。这是我当前的设置: public class IsHetAlBierTijdWidget extends AppWidgetProvider { static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int app
public class IsHetAlBierTijdWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = context.getString(R.string.app_name);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.is_het_al_bier_tijd_widget);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
sendEventToReactApp(context);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
public void sendEventToReactApp(Context context) {
WritableMap event = Arguments.createMap();
event.putString("message", "MyMessage");
ReactApplicationContext reactApplicationContext = new ReactApplicationContext(context);
MSREventBridgeModule msrEventBridgeModule = new MSREventBridgeModule(reactApplicationContext);
msrEventBridgeModule.emitEvent("Widget_Update",event);
}
}
反应代码:
class Home extends Component {
constructor(){
super();
this.onWidgetUpdate.bind(this);
}
onWidgetUpdate(){
console.log("widget update...");
}
componentWillMount() {
this.props.fetchBierTijd()
DeviceEventEmitter.addListener('Widget_Update', this.onWidgetUpdate);
}
componentWillUnmount() {
DeviceEventEmitter.removeListener('Widget_Update', this.onWidgetUpdate);
}
render() {
return (
<View style={{flex: 1, justifyContent:'center', alignItems:'center'}}>
<Countdown
style={{marginTop: 80}} fontSize="40"
isBiertijd={this.props.isBierTijd}
timeLeft={this.props.timeLeft}
/>
</View>
)
}
}
const mapStateToPros = state => ({
fetching: state.bierTijd.fetching,
isBierTijd: state.bierTijd.isBierTijd,
nextBierTijd: state.bierTijd.next,
timeLeft: moment(state.bierTijd.next).diff(moment())
})
const mapDispatchToProps = dispatch => {
return bindActionCreators({...bierTijdActions,}, dispatch)
}
export default connect(mapStateToPros, mapDispatchToProps)(Home)
class Home扩展组件{
构造函数(){
超级();
this.onWidgetUpdate.bind(this);
}
onWidgetUpdate(){
log(“小部件更新…”);
}
组件willmount(){
this.props.fetchBierTijd()
DeviceEventEmitter.addListener('Widget_Update',this.onWidgetUpdate);
}
组件将卸载(){
DeviceEventEmitter.removeListener('Widget_Update',this.onWidgetUpdate);
}
render(){
返回(
)
}
}
const mapStateToPros=状态=>({
获取:state.bierTijd.fetching,
isBierTijd:state.bierTijd.isBierTijd,
nextBierTijd:state.bierTijd.next,
timeLeft:moment(state.bierTijd.next).diff(moment())
})
const mapDispatchToProps=调度=>{
返回bindActionCreators({…bierTijdActions,},dispatch)
}
导出默认连接(mapStateToPros、mapDispatchToProps)(主)
是否可以这样做,或者是否需要从主活动触发事件?对于母语人士与react-native人士之间的交流,如果能给出一个很好的解释,我们将不胜感激。这绝对是可能的,您可以在这里找到指南:
您必须创建一个BroadcastReceiver(即WidgetProvider)和一个HeadlessJS任务来同时进行通信。也许还有其他方法,但至少这一种是有效的 widgetTask.js似乎没有反映编辑文件后的更改。不知道为什么