0

Im trying to launch an app that I've created from another app that Im working on right now. The thing is I've been searching throught the internet and found something but it did not work so Im seeking help in here.

This is what I've done on the app I want to launch from :

On my xaml.cs :

public async void GoToDigDitApp(object sender, EventArgs e)
        {
            var appname = "digdit://";
            var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);
        } 

I created an Interface:

    public interface IAppHandler
    {
        Task<bool> LaunchApp(string uri);
    }

In the Android project:


[assembly: Dependency(typeof(OpenAppAndroid))]
namespace SupervisingApp.Droid
{
    [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);
                Android.App.Application.Context.StartActivity(intent);


                result = true;
            }
            catch (ActivityNotFoundException)
            {
                result = false;
            }
            return Task.FromResult(result);
        }
    }
}

And This is the app I want to launch manifest :

<?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.Tab2" android:installLocation="auto">
   <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.READ_PHONE_STATE" />
   <uses-permission android:name="android.permission.CAMERA" />
   <application android:label="Dig Dit" android:icon="@drawable/ic_launcher">
       <activity android:icon="@drawable/ic_launcher" android:label="Dig Dit" android:name="digdit.urlentryclass">
           <intent-filter>
               <action android:name="android.intent.action.VIEW" />
               <category android:name="android.intent.category.DEFAULT" />
               <category android:name="android.intent.category.BROWSABLE" />
               <data android:scheme="digdit" />
           </intent-filter>
       </activity>
   </application>
</manifest>

For now Im only intressted by the Android part and it doesn't seem to work. I hope you guys can help me out.

Shahid Od
  • 103
  • 4
  • 13

2 Answers2

2

You could use PackageManager.GetLaunchIntentForPackage.

Try the code below. com.companyname.app2 is the package name of App2.

 Intent intent = PackageManager.GetLaunchIntentForPackage("com.companyname.app2");
            StartActivity(intent);

enter image description here

Updated:

Create a interface:

 public interface IDpendencyService
{
    Task<bool> Launch(string stringUri);
}

Implemention of Android:

 public class DependencyImplementation : Activity, IDpendencyService
{
    public Task<bool> Launch(string stringUri)
    {

        Intent intent = Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage(stringUri);
        if (intent != null)
        {
            intent.AddFlags(ActivityFlags.NewTask);
            Forms.Context.StartActivity(intent);
            return Task.FromResult(true);
        }
        else
        {
            return Task.FromResult(true);
        }



    }
}

Usage of MainPage:

<StackLayout>
    <!--  Place new controls here  -->
    <Label
        FontAttributes="Bold"
        FontSize="Large"
        HorizontalOptions="Center"
        Text="Welcome to App1!"
        VerticalOptions="CenterAndExpand" />
    <Button x:Name="GotoApp2" Text="GotoApp2" Clicked="GotoApp2_Clicked"></Button>
</StackLayout>


 private void GotoApp2_Clicked(object sender, EventArgs e)
    {
        DependencyService.Get<IDpendencyService>().Launch("com.companyname.app2");
    }

enter image description here

I have upload on GitHub, you could download from the StartAnotherApp_Xamarin.forms/App1 folder for reference. https://github.com/WendyZang/Test.git

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
  • I get this exception : Unhandled Exception: Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference – Shahid Od Feb 07 '20 at 12:23
  • I used this "com.companyname.Tab2" but it didn't work – Shahid Od Feb 08 '20 at 22:33
  • This app Tab2 need to be installed on device. The code works well on xamarin android. If you want to do this in xamarin.forms. You could use dependency service. – Wendy Zang - MSFT Feb 10 '20 at 04:09
1

You can open you app from xamarin.forms

Device.BeginInvokeOnMainThread(() =>
{
    Xamarin.Forms.Device.OpenUri(new Uri("digdit://555-1111"));
});
Anand
  • 1,866
  • 3
  • 26
  • 49