Xamarin上用于自定义文件扩展名的Intent-Filter实现 [英] Intent-Filter implementation for customized file Extensions on Xamarin

查看:153
本文介绍了Xamarin上用于自定义文件扩展名的Intent-Filter实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中打开带有自定义扩展名的文件.它可以来自电子邮件,下载,蓝牙等.每当用户点击这些文件时,我的应用程序便会打开并执行一些任务.我需要xamarin平台的参考/帮助.我真的是新来的.

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.

我的Android清单

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>

活动代码

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 ();
    }
}


}

遇到错误

无法实例化活动ComponentInfo {com.ppp.androidintentfile/com.ppp.androidintentfile.StartFileActivity}:java.lang.ClassNotFoundException:在路径:DexPathList [上找不到类"com.ppp.androidintentfile.StartFileActivity" zip文件"/data/app/com.ppp.androidintentfile-1/base.apk"],nativeLibraryDirectories=[/data/app/com.ppp.androidintentfile-1/lib/arm、/vendor/lib、/system/lib]]

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]]

推荐答案

使用类属性,而不添加任何硬编码的清单:

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>

如果您确实需要删除自动生成的名称,请提供一个硬编码的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

结果:

<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>

这篇关于Xamarin上用于自定义文件扩展名的Intent-Filter实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆