

<activity android:configChanges="orientation|screenSize" android:icon="@drawable/icon" android:label="OpenFiles.Droid" android:theme="@style/MyTheme" android:name="md5d37a78bf190038b83be9873b4223f8d1.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/pdf" /> </intent-filter> </activity> <intent-filter> node. These keywords help Android filter out applications that can perform certain operations. Here is an explanation of the values in this case of receiving the file:<action android:name="android.intent.action.SEND" /> - the Activity component is only going to respond to the implicit Intent, that is, to send data to other applications.<category android:name="android.intent.category.DEFAULT" /> is always necessary. Also, there should always be such a value to get an implicit Intent.<data android:mimeType="application/pdf" /> is a data type. Or the MIME type that the application can handle.IntentFilterAttribute class. Since there is only one Activity component in a Forms application, you need to MainActivity class with this attribute. To generate the XML in the AndroidManifest.xml file, identical to the one described above, the declaration of the MainActivity class should look like this: [Activity(Label = "OpenFiles.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] [IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = @"application/pdf")] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity {
There is also an ActivityAttribute class that will create an Activity node in the manifest. Then the IntentFilterAttribute class adds an Intent filter to this Activity node. Full documentation on the IntentFilterAttribute class can be found here . It defines the action, category and type of incoming data. Action and category are arrays, and they can be defined. They may also contain parameters that help filter by matching criteria. With this add-on, the Android class recognizes an application as capable of handling PDF files.OnCreate function in the MainActivity . The Activity Intent property contains information that is needed to get the base file. While retrieving the file, you can expect the necessary information in the ClipData object of the ClipData property.Android.Net.Uri , which is used by ContentResolver to start the data stream. You can then save this data stream in the application as a local file.Intent.ExtraStream .Uri received from, it is necessary to make sure that the file is saved in the application. In this case, there will be confidence that the file will have full access.OnCreate shown below. App _mainForms; protected override void OnCreate(Bundle bundle) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(bundle); global::Xamarin.Forms.Forms.Init(this, bundle); var mainForms = new App(); LoadApplication(mainForms); if (Intent.Action == Intent.ActionSend) { // Extras var uriFromExtras = Intent.GetParcelableExtra(Intent.ExtraStream) as Android.Net.Uri; var subject = Intent.GetStringExtra(Intent.ExtraSubject); // ClipData var pdf = Intent.ClipData.GetItemAt(0); // URI var pdfStream = ContentResolver.OpenInputStream(pdf.Uri); // var memOfPdf = new System.IO.MemoryStream(); pdfStream.CopyTo(memOfPdf); var docsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); var filePath = System.IO.Path.Combine(docsPath, "temp.pdf"); System.IO.File.WriteAllBytes(filePath, memOfPdf.ToArray()); mainForms.DisplayThePDF(filePath); } } OnCreate . The significant difference is that after calling Xamarin.Forms LoadApplication , it checks if the Intent was loaded with information from another application, causing the implicit Intent.
If it finds data in the Intent property that receives the Intent.ActionSend action (that is, the file is sent to the application), the file is copied to the application's local storage, then the DisplayThePDF() function is DisplayThePDF() .DisplayThePDF() displays a modal page in a web browser, and this web browser displays the downloaded PDF file. This browser works through a custom renderer. The code for this renderer is written using the Xamarin documentation .MainActivity class in an Android project in Xamarin.Forms using the IntentFilterAttribute . After that, the corresponding markup is placed in the AndroidManifest.xml file, and now all you need to do is to process the incoming data using the OnCreate() function, which ideally will simply transfer the work of the function already in the Xamarin.Forms project.
Alexander Alekseev - Xamarin-developer, freelancer. Works with the .NET platform since 2012. Participated in the development of a procurement automation system at Digamma. C 2015 went into freelancing and switched to mobile development using Xamarin. Currently working at StecPoint on an iOS application.Source: https://habr.com/ru/post/324796/
All Articles