英文:
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
中添加IntentFilter
和Exported = 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 = "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());
}
}
Note: remember to add code DataSchemes = new[] { "myapp" }
.
For the current app, you need to add queries
tag for the app you want to open in file manifest.xml
.
For example:
<?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>
And usage example:
public async void OpenSecondApp()
{
var supportsUri = await Launcher.Default.CanOpenAsync("myapp://");
if (supportsUri)
await Launcher.Default.OpenAsync("myapp://com.xamarin.secondapp");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论