如何获得意图过滤MimeType

时间:2013-12-14 18:58:22

标签: android android-intent mime-types ndef

我的应用程序中有以下过滤器。我希望能够使用三种不同的mimeTypes启动相同的应用程序。

后来我读了NDEF消息,但是如何检查用于启动应用程序的mime类型,以便我可以相应地处理NDEF消息数据?

        <intent-filter>
               <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
               <category android:name="android.intent.category.DEFAULT"/>
               <data android:mimeType="text/product" />
        </intent-filter>
        <intent-filter>
               <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
               <category android:name="android.intent.category.DEFAULT"/>
               <data android:mimeType="text/pesticide" />
        </intent-filter>
        <intent-filter>
               <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
               <category android:name="android.intent.category.DEFAULT"/>
               <data android:mimeType="text/seed" />
        </intent-filter>

1 个答案:

答案 0 :(得分:3)

以下是我在应用中处理csv文件和文本文件的简化版本。我希望这有帮助:

@Override
public void onNewIntent(final Intent intent)
{
    super.onNewIntent(intent);
    String type = intent.getType();
    String action = intent.getAction();
    if ("text/csv".equals(type) || "text/comma-separated-values".equals(type))
    {
        // Handle CSV file being sent
        handleSendCSV(intent); 
    } 
    else if("text/plain".equals(type) && Intent.ACTION_SEND.equals(action))
    {
        // Handle plaintext sent
        handlePlainText(intent);
    } 
    else
    {
        //Alert of some error
        doAlertDialog("Error.", "Invalid file type.");
    }
}

编辑 -

添加了:

String action = intent.getAction();

所以代码就完整了。