需要在应用程序之间进行导航

huangapple go评论72阅读模式
英文:

Need to navigate between apps

问题

这是我的Maui中的按钮单击事件。但是supportUri 变量的值是 false

我需要在Android中添加数据方案,以使Uri返回 true 并导航。

英文:
private async void GoApp2Clicked(object sender, EventArgs e)
{
    //var uri = new Uri("app2://");
    //bool isOpened = await Launcher.TryOpenAsync(uri);
    bool supportsUri = await Launcher.Default.CanOpenAsync("app2://");

    if (supportsUri)
        await Launcher.Default.OpenAsync("app2://com.companyname.app2");
}

This is my button click event in maui.But the supportUri is getting false value

I need to add the data scheme in android for getting the Uri true and navigate..

答案1

得分: 1

假设你要打开的应用程序的包名是com.xamarin.secondapp

然后,你需要在要打开的应用程序(com.xamarin.secondapp)的MainActivity.cs中添加IntentFilterExported = true,如下所示:

[Activity(Label = "SecondApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, Exported = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[
    IntentFilter
    (
        new[] { Android.Content.Intent.ActionView },
        Categories = new[]
            {
                Android.Content.Intent.CategoryDefault,
                Android.Content.Intent.CategoryBrowsable
            },
        DataSchemes = new[] { "myapp" }
    )
]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }
}

注意: 记得添加代码 DataSchemes = new[] { "myapp" }

对于当前应用程序,你需要在manifest.xml文件中为要打开的应用程序添加queries标签。

例如:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.openappapp1">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" />
    <application android:label="OpenappApp1.Android" android:theme="@style/MainTheme">
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <queries>
        <package android:name="com.xamarin.secondapp" />
    </queries>
</manifest>

用法示例:

public async void OpenSecondApp()
{
    var supportsUri = await Launcher.Default.CanOpenAsync("myapp://");
    if (supportsUri)
        await Launcher.Default.OpenAsync("myapp://com.xamarin.secondapp");
}
英文:

Suppose the package name of the app you want to open is com.xamarin.secondapp.

Then you need to add IntentFilter and Exported =true to MainActivity.cs of the app you want to open(com.xamarin.secondapp).

Just as follows:

  [Activity(Label = &quot;SecondApp&quot;, Icon = &quot;@mipmap/icon&quot;, Theme = &quot;@style/MainTheme&quot;, MainLauncher = true,Exported =true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
    [
    IntentFilter
    (
        new[] { Android.Content.Intent.ActionView },
        Categories = new[]
            {
                Android.Content.Intent.CategoryDefault,
                Android.Content.Intent.CategoryBrowsable
            },
        DataSchemes = new[] { &quot;myapp&quot; }
    )
]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
    }

Note: remember to add code DataSchemes = new[] { &quot;myapp&quot; }.

For the current app, you need to add queries tag for the app you want to open in file manifest.xml.

For example:

  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; 
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:versionCode=&quot;1&quot; android:versionName=&quot;1.0&quot; package=&quot;com.companyname.openappapp1&quot;&gt;
    &lt;uses-sdk android:minSdkVersion=&quot;21&quot; android:targetSdkVersion=&quot;33&quot; /&gt;
    &lt;application android:label=&quot;OpenappApp1.Android&quot; android:theme=&quot;@style/MainTheme&quot;&gt;

      &lt;/application&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;


      &lt;queries&gt;
            &lt;package android:name=&quot;com.xamarin.secondapp&quot; /&gt;
      &lt;/queries&gt;
&lt;/manifest&gt;

And usage example:

public async void OpenSecondApp()
{

    var supportsUri = await Launcher.Default.CanOpenAsync(&quot;myapp://&quot;);
    if (supportsUri)
        await Launcher.Default.OpenAsync(&quot;myapp://com.xamarin.secondapp&quot;);

}

huangapple
  • 本文由 发表于 2023年3月7日 15:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定