Issue
I'm new on MAUI and I need my MAUI app receiving a json file shared from other apps on Android. I searched a lot but have no luck at all. No found guides work.
I tried a dedicated activity class, then tried moving those code into MainActivity. Neither works.
Here are what I did on my app:
In AndroidManifest.xml
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" android:label="KitCare">
<activity android:name="KitCare.DataFileIntentActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/json" />
</intent-filter>
</activity>
</application>
In DataFileIntentActivity.cs:
namespace KitCare
{
[Activity(Name = "KitCare.DataFileIntentActivity", Exported = true),
Theme = "@style/MyAppTheme" //Requested by Jessie Zhang. Theme name is the same as the main activity.
]
[IntentFilter(
new[] { Android.Content.Intent.ActionSend },
Categories = new[] { Android.Content.Intent.CategoryDefault },
DataMimeType = "application/json")]
public class DataFileIntentActivity : MauiAppCompatActivity
{
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
//Requested by Jessie Zhang
Platform.Init(this, savedInstanceState);
OnNewIntent(Intent);
}
protected override void OnNewIntent(Intent? intent)
{
base.OnNewIntent(intent);
if (Intent?.Action == Intent.ActionSend)
{
Stream? inputStream = null;
var filePath = Intent?.ClipData?.GetItemAt(0);
if (filePath?.Uri != null)
{
inputStream = ContentResolver!.OpenInputStream(filePath.Uri)!;
}
if (inputStream != null)
{
using (var reader = new StreamReader(inputStream))
{
var content = reader.ReadToEnd();
//process the content here...
}
inputStream.Close();
inputStream.Dispose();
}
}
}
}
}
Currently, I can see my app listed as a target when a file is selected for sharing. But after the app is selected, an empty UI with program title is shown and quit immediately.
Any ideas? Thanks a lot.
Solution
You can try to add your code to function OnCreate
.
Please refer to the following code:
[Activity(Label = "FormSecondApp", Theme = "@style/MainTheme", MainLauncher = true, Exported = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter
(
new[] { Android.Content.Intent.ActionView, Android.Content.Intent.ActionSend },
Categories = new[]
{
Android.Content.Intent.CategoryDefault
},
DataMimeType = "application/json"
)]
public class MainActivity : MauiAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
if (Intent?.Action == Android.Content.Intent.ActionSend)
{
Stream? inputStream = null;
var filePath = Intent?.ClipData?.GetItemAt(0);
if (filePath?.Uri != null)
{
inputStream = ContentResolver!.OpenInputStream(filePath.Uri)!;
}
if (inputStream != null)
{
using (var reader = new StreamReader(inputStream))
{
var content = reader.ReadToEnd();
//process the content here...
}
inputStream.Close();
inputStream.Dispose();
}
}
}
}
Answered By - Jessie Zhang -MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.