Intent-Filter implementation for customized file E

2019-05-28 22:10发布

问题:

I want to open files with customized extensions in my app. It can come from email, downloads, Bluetooth etc. Whenever user taps on those files my application should open and perform some tasks. I need a reference/help for xamarin platform. I am literally new in this.

My android menifest

<activity android:name=".StartFileActivity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="content" />
            <data android:pathPattern=".*\\.IE3" />
            <data android:mimeType="application/*" android:scheme="content" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="http" android:host="com.ppp.androidintentfile" />
            <data android:host="*" />
            <data android:pathPattern=".*\\.IE3" />
            <data android:pathPattern=".*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.IE3" />
            <data android:mimeType="*/*" android:scheme="content" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="https"  android:host="com.ppp.androidintentfile" />
            <data android:host="*" />
            <data android:pathPattern=".*\\.IE3" />
            <data android:pathPattern=".*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.IE3" />
            <data android:mimeType="*/*" android:scheme="content" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="file"  android:host="com.ppp.androidintentfile" />
            <data android:host="*" />
            <data android:pathPattern=".*\\.IE3" />
            <data android:pathPattern=".*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.IE3" />
            <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.IE3" />
            <data android:mimeType="*/*" android:scheme="content" />
        </intent-filter>
    </activity>

Activity Code

namespace AndroidIntentFile
{
[Activity (Label = "StartFileActivity")]
[IntentFilter(new[]{Intent.ActionSend},Categories = new[]{Intent.CategoryDefault},DataMimeType = "application/*",Label = "AndroidIntentFile")]  

public class StartFileActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        if (Intent.Action == Intent.ActionSend && Intent.Extras.ContainsKey(Intent.ExtraStream))
        {
            var fileUrl = GetFilePath((Android.Net.Uri)Intent.Extras.GetParcelable(Intent.ExtraStream));
        }
    }

    private string GetFilePath(Android.Net.Uri uri)
    {
        return uri.ToString ();
    }
}


}

Getting below error

Unable to instantiate activity ComponentInfo{com.ppp.androidintentfile/com.ppp.androidintentfile.StartFileActivity}: java.lang.ClassNotFoundException: Didn't find class "com.ppp.androidintentfile.StartFileActivity" on path: DexPathList[[zip file "/data/app/com.ppp.androidintentfile-1/base.apk"],nativeLibraryDirectories=[/data/app/com.ppp.androidintentfile-1/lib/arm, /vendor/lib, /system/lib]]

回答1:

Using class attributes and without making any hardcoded manifest additions:

[Activity(Label = "StartFileActivity")]
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "application/*", DataPathPattern = "*.IE3")]
public class StartFileActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Do something with the *.IE3 file...
    }
}

Will automatically add to your manifest:

<activity android:label="StartFileActivity" android:name="md54385a510f3a446695f2c9f6ad6a86f05.StartFileActivity">
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="application/*" />
    <data android:pathPattern="*.IE3" />
  </intent-filter>
</activity>

If you really need to remove the auto-generated name, provide a hard-code Activity name:

[Activity(Label = "StartFileActivity", Name="foo.bar.SomeName")]
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "application/*", DataPathPattern = "*.IE3")]
public class StartFileActivity : Activity

Results in:

<activity android:label="StartFileActivity" android:name="foo.bar.SomeName">
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="application/*" />
    <data android:pathPattern="*.IE3" />
  </intent-filter>
</activity>