【问题标题】:Open React Native component from Native Java Activity in Android在 Android 中从 Native Java Activity 打开 React Native 组件
【发布时间】:2018-05-02 15:47:57
【问题描述】:

如何从 Java 中的原生 Android Activity 打开我的 React Native 应用程序的特定组件?

这是我的用例:
我在我的 android 部分注册了一个通知接收器服务,一个 android 活动(命名为 Activity1)在动作中分配,当收到通知时立即打开(无论应用程序是打开还是关闭)。现在如何从 Activity(Activity1) 打开 React Native 组件?

【问题讨论】:

标签: java android react-native push-notification onesignal


【解决方案1】:

我读到'Integration with Android Existing Apps Doc' 它是开放根组件(使用捆绑包),而不是特定组件。

我必须在屏幕关闭(锁定屏幕)时打开特定的反应原生 js 组件

**我使用expo framework

   public class UnlockActivity extends ReactActivity implements DefaultHardwareBackBtnHandler {
    //static constants


    //
    private ReactRootView mReactRootView;
    private ReactInstanceManager mReactInstanceManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mReactRootView = new ReactRootView(this);
        mReactInstanceManager = ((MainApplication) getActivity().getApplication())
                .getReactNativeHost()
                .getReactInstanceManager();
        // The string here (e.g. "MyReactNativeApp") has to match
        // the string in AppRegistry.registerComponent() in index.js
        mReactRootView.startReactApplication(mReactInstanceManager, "LockScreen", null);

        setContentView(mReactRootView);
    }

接收者

public class LockScreenReceiver extends BroadcastReceiver {
//Member Variable
private TelephonyManager telephonyManager   =   null;
private boolean isPhoneIdle                 =   true;

private PhoneStateListener phoneListener    =   new PhoneStateListener()
{
    @Override
    public void onCallStateChanged(int state, String incomingNumber)
    { //Member Object
        switch(state){
            case TelephonyManager.CALL_STATE_IDLE :
                isPhoneIdle = true;
                break;
            case TelephonyManager.CALL_STATE_RINGING :
                isPhoneIdle = false;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK :
                isPhoneIdle = false;
                break;
        }
    }
};



//Construc

tor
    public LockScreenReceiver() {
    }



    //Override Method
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
        {
            if(telephonyManager == null)
            {
                telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
            }


            if(isPhoneIdle)
            {
                Intent i = new Intent(context, UnlockActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }


        }
    }
}

【讨论】:

  • 有解决办法吗?
  • 这个问题有什么解决办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 2017-02-13
  • 2017-05-21
  • 1970-01-01
相关资源
最近更新 更多