android:React native从另一个应用程序打开一个应用程序?

时间:2018-10-18 10:39:55

标签: javascript android react-native

我正在尝试从我的应用程序打开另一个应用程序(https://play.google.com/store/apps/details?id=com.inova.velocity)。但是所有教程都只将url重定向到playstore。(我找到了github链接(https://github.com/FiberJW/react-native-app-link),它仅打开iOS的应用程序,但对于Android则将其重定向到playstore)。有什么办法可以解决这个问题?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Threading;

namespace WindowsFormsApplication23
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] cities = { "Los Angeles", "New York", "Salem", "Portland", "Washington" };
            DataTable dt = GetWeather(cities);
            dataGridView1.DataSource = dt;
        }

        DataTable GetWeather(string[] cities)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("City", typeof(string));
            dt.Columns.Add("Region", typeof(string));
            dt.Columns.Add("Humidity", typeof(int));
            dt.Columns.Add("Wind Speed", typeof(int));
            dt.Columns.Add("Temperature", typeof(int));
            dt.Columns.Add("Condition", typeof(string));
            dt.Columns.Add("High Temperature", typeof(int));
            dt.Columns.Add("Low Temperature", typeof(int));

            foreach (string inputCity in cities)
            {
                String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(100) where text='{0}')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", inputCity);
                XDocument wData = XDocument.Load(query);
                XNamespace ns = wData.Root.GetDefaultNamespace();
                //System.Threading.Thread.Sleep(10000);
                foreach (XElement channel in wData.Descendants().Where(x => x.Name.LocalName == "channel"))
                {
                    string city = "";
                    string region = "";
                    int? humidity = null;
                    int? speed = null;
                    int? temp = null;
                    string tfCond = "";
                    int? high = null;
                    int? low = null;

                     XNamespace yWeatherNs = channel.Elements().First().GetNamespaceOfPrefix("yweather");

                    XElement xLocation = channel.Element(yWeatherNs + "location");
                    if (xLocation == null)
                    {
                        continue;
                    }
                    else
                    {
                        city = (string)xLocation.Attribute("city");
                        region = (string)xLocation.Attribute("region");
                    }

                    XElement xAtmosphere = channel.Element(yWeatherNs + "atmosphere");
                    if (xAtmosphere != null)
                    {
                        humidity = (int)xAtmosphere.Attribute("humidity");
                    }

                    XElement xWind = channel.Element(yWeatherNs + "wind");
                    if (xWind != null)
                    {
                        speed = (int)xWind.Attribute("speed");
                    }

                    XElement item = channel.Element("item");
                    if (item != null)
                    {

                        XElement xCondition = item.Element(yWeatherNs + "condition");
                        if (xCondition != null)
                        {
                            temp = (int)xCondition.Attribute("temp");
                        }


                        List<XElement> xForecast = item.Elements(yWeatherNs + "forecast").ToList(); ;
                        if (xForecast != null)
                        {
                            tfCond = (string)xForecast.FirstOrDefault().Attribute("text");
                            high = (int)xForecast.FirstOrDefault().Attribute("high");
                            low = (int)xForecast.FirstOrDefault().Attribute("low");
                        }
                    }

                    dt.Rows.Add(new object[] { city, region, humidity, speed, temp, tfCond, high, low });
                }                
            }
            dt = dt.AsEnumerable().OrderBy(x => x.Field<string>("City")).ThenBy(x => x.Field<string>("Region")).CopyToDataTable();
            return dt;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

是的,您的代码是正确的。但是您使用的是Playstore网址,而不是架构网址。您必须设置schemaUrl,您可以从相关的应用程序开发人员那里获取它。如果没有为该应用设置架构网址,则无法打开它。获得SchemaUrl后,您可以使用您的代码。如下所示。


Linking.canOpenURL(SchemaUrl).then(supported => {
             if (supported) {
               console.log('accepted');
               return Linking.openURL(SchemaUrl);
             } else {
               console.log('an error occured');
             }
           }).catch(
             err => console.log('an error occured');
           );

答案 1 :(得分:-1)

使用react-native-send-intent module可以执行
SendIntentAndroid.openApp('packagename')。then((wasOpened)=> {});您的软件包名称是您要打开的任何应用程序软件包名称。

例如 SendIntentAndroid.openApp('com.inova.velocity')。then((wasOpened)=> {});

wasOpened是一个布尔型诺言,告诉您应用程序是否已打开