我有2个使用Xamarin.Forms开发的应用程序(A和B)。我需要从应用B打开应用A。
我已经按照以下thread尝试过以下操作:
在视图中
var appname = @"otherappId";
var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);
界面
public interface IAppHandler
{
Task<bool> LaunchApp(string uri);
}
Android
[Activity(Label = "OpenAppAndroid")]
public class OpenAppAndroid : Activity, IAppHandler
{
public Task<bool> LaunchApp(string uri)
{
bool result = false;
try
{
var aUri = Android.Net.Uri.Parse(uri.ToString());
var intent = new Intent(Intent.ActionView, aUri);
Xamarin.Forms.Forms.Context.StartActivity(intent);
result = true;
}
catch (ActivityNotFoundException)
{
result = false;
}
return Task.FromResult(result);
}
}
IOS
public class OpenAppiOS : IAppHandler
{
public Task<bool> LaunchApp(string uri)
{
try
{
var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(uri));
if (!canOpen)
return Task.FromResult(false);
return Task.FromResult(UIApplication.SharedApplication.OpenUrl(new NSUrl(uri)));
}
catch (Exception ex)
{
return Task.FromResult(false);
}
}
对于android系统,我在运行项目时得到System.NullReferenceException: 'Object reference not set to an instance of an object.'
。我不知道代码中的应用名称是什么?我已尝试使用包名称。
此外,如果设备上未安装android应用,则需要打开Play商店下载该应用。
我没有测试iOS部件,因为Mac不可用。上面的代码适用于iOS吗?另外,如果未在iPhone上安装该应用程序,则需要打开AppStore来下载该应用程序。
此外,我想为UWP实现相同的功能。
参考: https://forums.xamarin.com/discussion/92666/detect-and-open-another-app-on-device Is it possible to open another app in my app with xamarin.form?
答案 0 :(得分:3)
Xamarin.Forms.Forms.Context
自XF 2.5起已过时。因此,您可以使用Android.App.Application.Context
代替它,也可以使用插件Plugin.CurrentActivity
因此您可以像下面那样修改Android中的代码
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App14;
using App14.Droid;
using Xamarin.Forms;
[assembly: Dependency(typeof(OpenAppAndroid))]
namespace App14.Droid
{
public class OpenAppAndroid : IAppHandler
{
public Task<bool> LaunchApp(string packageName)
{
bool result = false;
try
{
PackageManager pm = Android.App.Application.Context.PackageManager;
if (IsAppInstalled(packageName))
{
Intent intent = pm.GetLaunchIntentForPackage(packageName);
if (intent != null)
{
intent.SetFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
else
{
Intent intent = pm.GetLaunchIntentForPackage("the package name of play store on your device");
if (intent != null)
{
intent.SetFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
}
catch (ActivityNotFoundException)
{
result = false;
}
return Task.FromResult(result);
}
private bool IsAppInstalled(string packageName)
{
PackageManager pm = Android.App.Application.Context.PackageManager;
bool installed = false;
try
{
pm.GetPackageInfo(packageName, PackageInfoFlags.Activities);
installed = true;
}
catch (PackageManager.NameNotFoundException e)
{
installed = false;
}
return installed;
}
}
}
对于iOS,您可以参考Launch Another IOS App from Xamarin Forms App。
答案 1 :(得分:1)
您是否尝试过使用Xamarin Essentials: Xamarin.Essentials: Launcher
public class LauncherTest
{
public async Task OpenRideShareAsync()
{
var supportsUri = await Launcher.CanOpenAsync("lyft://");
if (supportsUri)
await Launcher.OpenAsync("lyft://ridetype?id=lyft_line");
}
}
或
Device.OpenUri(new Uri("fb://page/page_id"));
Device.OpenUri(new Uri("twitter://user?user_id=userid"))
打开特定应用时不要忘记使用正确的软件包名称