Reputation: 11
I'm new in the channel; I recently started with xamarin.form for cross-plattform apps.
I've been stuck with a question for a few days. I would like to know if there is a method that allow me to open a third-party application by my app or it's necessary to implement this function for each platform in a different way.
Precisely, I would like to know if there is a method similar to that provided by IFrame in UIWebview for web pages to open third-party apps in my app without launching the second app sending my app in background?
Upvotes: 1
Views: 4100
Reputation: 1
This works very well for me.
In xamarin forms in the layout file put this button
<Button Text="Open Facebook"
Margin="20"
Clicked="Button_Clicked"
VerticalOptions="CenterAndExpand"
HorizontalOptions="FillAndExpand"
BackgroundColor="#1b89c0"
TextColor="White"/>
Next the .cs of this layout, I have the button and implementing the dependency service.
async void Button_Clicked(System.Object sender, System.EventArgs e)
{
DependencyService.Get<IBackgroundDependency>().ExecuteCommand();
}
The next step is make a new interface
public interface IBackgroundDependency
{
void ExecuteCommand();
}
and finish with the class inside the android directory.
[assembly: Dependency(typeof(BackgroundDependency_Android))]
namespace AbrirApp.Droid{
public class BackgroundDependency_Android : Java.Lang.Object, IBackgroundDependency
{
public void ExecuteCommand()
{
Intent intent = new Intent();
intent.SetFlags(ActivityFlags.NewTask);
intent.SetComponent(new ComponentName("com.example.averesta", "com.example.averesta.MainActivity"));
Android.App.Application.Context.StartActivity(intent);
}
}
}
new ComponentName("Package name", "Package name activity")
In my case the main application is in xamarin, and the invoked application is in android studio.
Upvotes: 0
Reputation: 23
you can try this;
private async void Button_OnClicked(object sender, EventArgs e)
{ //You need to change your app package address which you want to open with button. Example: "com.microsoft.office.officelens"
Intent intent = new Intent();
intent.SetComponent(new ComponentName("com.package.address", "com.package.address.MainActivity"));
Android.App.Application.Context.StartActivity(intent);
}
Also, if you want to check user downloaded or didn't download this app;
public string facebookApp;
public async Task ShowAlert()
{
this.facebookApp = "https://play.google.com/store/apps/details?id=com.facebook.katana&hl=tr";
var action = await DisplayAlert("Warning", "Please download facebook app first.", "Okay", "Cancel");
if (action)
{
Device.OpenUri(new Uri(this.facebookApp));
}
}
private async void Button_OnClicked(object sender, EventArgs e)
{
try
{
Intent intent = new Intent();
intent.SetComponent(new ComponentName("com.package.address", "com.package.address.MainActivity"));
Android.App.Application.Context.StartActivity(intent);
}
catch (Exception ex)
{
ShowAlert();
}
}
So, in this case; if the user didn't download xApp(facebook, twitter, etc.) before, he/she will get a warning. If they click Okay on DisplayAlert, the program will send them to Google Play link.
Upvotes: 1