首页 > 解决方案 > 从 React 本机应用程序打开外部应用程序(按钮单击)

问题描述

我想通过单击我使用过的 Button 打开 New React native App

在 React Native 中链接概念

React native Code : Test 是 Other App 的名称

Linking.openURL('Test://app');

还遵循在 android.manifest.xml 文件中添加 Intent 的 URL Andriod Intent

代码:Android.manifestfile.xml

  <activity
        android:name=".MainActivity"
        android:launchMode="singleTask"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
          <intent-filter>
              <action android:name="android.intent.action.VIEW" />
              <category android:name="com.Test" />
              <category android:name="android.intent.category.BROWSABLE" />
              <data android:scheme="Test" android:host="app" />
          </intent-filter>
      </activity>

我该如何解决这个问题?

标签: javascriptandroidreactjsreact-native

解决方案


将此代码添加到与当前意图过滤器并行的 AndroidManifest.xml 文件中

 <intent-filter android:label="@string/app_name">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with "example://gizmos” -->
        <data android:scheme="app"
              android:host="testApp" />
    </intent-filter> 

并运行此命令React-native run-android

将以下代码添加到您的 react-native 文件中。

<Button title="Click me" onPress={ ()=>{ Linking.openURL('app://testApp')}} />

为了节省时间。您可以在同一应用程序中同时编写代码,并且同一应用程序将在按下按钮时打开

我只是试试这段代码,它对我有用,让我知道是否仍然面临问题(Y)


推荐阅读