Xamarin

时间:2016-06-07 19:52:29

标签: android-intent xamarin xamarin.android

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

我的机器人清单

<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:未在路径上找到类“com.ppp.androidintentfile.StartFileActivity”:DexPathList [[ zip文件“/data/app/com.ppp.androidintentfile-1/base.apk"],nativeLibraryDirectories=[/data/app/com.ppp.androidintentfile-1/lib/arm,/ vendor / lib,/ system / LIB]

1 个答案:

答案 0 :(得分:3)

使用类属性并且不添加任何硬编码清单:

[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...
    }
}

会自动添加到您的清单中:

<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名称:

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