Hello everyone.
I am experiencing a problem with an Android Intent. My app can open an email attachment/files with an .afsp extension.
When I open this in iOS, it reads the content of the file, sends it to my Forms project and displays the data to the user using my AppointmentDetailPage.
When I open this in Android, it reads the content of the file, sends it to my Forms project, keeps hanging on the intent screen and opens the AppointmentDetailPage
in the background. Here's a screenshot: (there's the "Bijlage opener", the Activity that launches when I open the attachment AND there's the "Bezoekers" app, which is my app and has to be displayed to the user with the AppointmentDetailPage
. As you can see, there are 2 apps open.
This is my android Intent code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Newtonsoft.Json;
using Bezoekers.Model;
using System.IO;
using Xamarin.Forms;
using System.Security.Cryptography;
using Bezoekers;
using Bezoekers.Util;
namespace Bezoekers.Droid
{
/* An intent: http://developer.android.com/guide/components/intents-filters.html
*
* When Android knows that the user wants to open a file, it searches for apps to open the file.
* It sees what kind of Intent it is going to send to an app, and check's the app's intent filters for possible matches.
* (The intents are mentioned in AndroidManifest.XML (In Medewerkers.Droid/obj/Debug/android/AndroidManifest.XML)
* The Intent.ActionView means it is going to open a view and show data to a user. For example, you could also create a filter using ActionSend to share an item.
* The Categories mean that there are different types of Intents that belong together.
* MimeType is the type of the file. I use 'text/afsp' for File browsers, and application/octet-stream for email apps etc.
* DataPathPattern means that the file needs to have a certain extension in order to allow our app to open the file.
*/
[Activity (Label = "Bijlage Opener")]
[IntentFilter (new[]{Intent.ActionView},
Categories=new[]{Intent.CategoryBrowsable, Intent.CategoryDefault},
DataMimeType= "text/afsp",
DataPathPattern= "*.afsp",
DataHost="*")]
[IntentFilter (new[]{Intent.ActionView},
Categories=new[]{Intent.CategoryBrowsable, Intent.CategoryDefault},
DataMimeType= "application/octet-stream",
DataPathPattern= "*.afsp",
DataHost="*")]
public class AttachmentOpener_Android : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
var _intent = Intent;
Appointment decryptedAppointment = (_intent.Data.Scheme == "content") ? GetAppointmentFromContent (_intent.Data) : GetAppointmentFromFile (_intent.Data.Path);
Xamarin.Forms.DependencyService.Get<INativeToForms> ().OpenDetailPage(decryptedAppointment);
}
private Appointment GetAppointmentFromFile(string path)
{
Appointment tempAppointment = new Appointment();
string jsonDecryptString = string.Empty;
using(var streamReader = new StreamReader(path))
{
while ((jsonDecryptString = streamReader.ReadLine ()) != null) {
tempAppointment = JsonConvert.DeserializeObject<Appointment>(Encryptor.Decrypt(jsonDecryptString));
}
}
return tempAppointment;
}
private Appointment GetAppointmentFromContent(Android.Net.Uri uri)
{
Appointment tempAppointment = new Appointment();
string jsonDecryptString = string.Empty;
Stream stream = ContentResolver.OpenInputStream (Intent.Data);
using (var streamReader = new StreamReader (stream)) {
while ((jsonDecryptString = streamReader.ReadLine ()) != null) {
tempAppointment = JsonConvert.DeserializeObject<Appointment> (Encryptor.Decrypt(jsonDecryptString));
}
}
return tempAppointment;
}
}
}
As you can see, I use the dependencyService to send the object to my Forms project.
This is the code where I receive my object:
using System;
using Xamarin.Forms;
[assembly: Xamarin.Forms.Dependency(typeof(Bezoekers.FormsDependencyImpl))]
namespace Bezoekers
{
public class FormsDependencyImpl : INativeToForms
{
public FormsDependencyImpl ()
{
}
public void OpenDetailPage (Bezoekers.Model.Appointment appointment)
{
App.Database.SaveAppointment (appointment);
var appointmentPage = new AppointmentDetailPage (appointment);
var mainScreen = Application.Current.MainPage as MainScreen;
if (mainScreen != null) {
var navPage = mainScreen.Detail as NavigationPage;
if (navPage != null) {
navPage.PushAsync (appointmentPage, true);
}
}
}
}
}
As I said, it pushes the appointmentPage to the app, but it doesn't get displayed. The intent screen just stays there.
Can anyone tell me what I am doing wrong? I think it's because of my Intent, but I'm not sure. If you need more code from my code project, please tell me.
Thank you!