Open React Native component from Native Java Activity in Android

Viewed 1744

How can I open a specific component of my React Native application from an Native Android Activity in Java?

Here is my use case:
I'm registering a notification receiver service in my android part, an android activity(name it Activity1) is assigned in action which is immediately opened when the notification is received(no matter app is open or closed). Now how can I open a React Native component from the Activity(Activity1)?

1 Answers

I read 'Integration with Android Existing Apps Doc' it is open root component(using bundle), not specific component.

i have to open specific react native js component at screen-off(lock screen)

**i using 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);
    }

Receiver

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);
            }


        }
    }
}
Related