0

My app is programmed in Xamarin and I want allow the user to open pdf in my app.

Currently, in Android when the user clicks on an attachment in Gmail that has a PDF there's a dialog titledComplete the action usingthat allows the user to choose an app with which the phone opens thePDF` file. How do I get listed in that dialog via Xamarin (ideally in a way that also works on other platforms)?

Christian
  • 25,249
  • 40
  • 134
  • 225
  • 1
    There you go ;) [https://stackoverflow.com/questions/3465429/register-to-be-default-app-for-custom-file-type](https://stackoverflow.com/questions/3465429/register-to-be-default-app-for-custom-file-type) – Daniel Oct 24 '18 at 13:42
  • similar question https://stackoverflow.com/questions/51556376/setting-my-app-as-a-sharing-target-xamarin/51560082#51560082 – R15 Oct 25 '18 at 11:52

2 Answers2

2

You have to update your manifest to tell Android that you can handle pdf files.

        <intent-filter >
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="application/pdf" />
        </intent-filter>
Daniel
  • 9,312
  • 3
  • 48
  • 48
  • I understand how I would mark an activity this way in standard Android but I'm unsure how it works in Xamarin given Xamarin seems to automatically generate the code for the activities in the manifest. Can you write more about how this is supposed to look in Xamarin? – Christian Oct 25 '18 at 07:35
0

You can create a new class derived from Activity. You'll have to annotate this class with the ActivityAttribute as well as th IntentFilterAttribute in order to make it available to the system for opening PDFs. Basically the IntentFilter is what Daniel described, but rather annotated directly on a class, instead of XML defined.

[Activity(Label = "PdfActivity")]
[IntentFilter(new[] { Intent.ActionView }, 
              Categories = new[] { Intent.CategoryDefault }, 
              DataMimeType = "application/pdf")]
public class PdfActivity : Activity
{
    /// <summary>
    /// Is called when a PDF shall be opened.
    /// </summary>
    /// <param name="savedInstanceState">The saved instance state.</param>
    protected override void OnCreate(Bundle savedInstanceState)
    {
        // do whatever you have to do
    }
}
Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
  • If i launch my app using gmail pdf attachment, do i get intent action as VIEW inside my onCreate() – Query Dec 03 '18 at 19:20