2014-11-06 208 views
0

我创建了一个用于播放MP3的应用程序。现在我想从其他应用程序如文件浏览器中打开我的应用程序。 当用户点击任何MP3文件,它必须表现出我的应用程序“完成操作使用”这样的:从其他应用程序打开您的应用程序

使用完成操作:

[我的应用程序]

[其他应用1]

[其他应用2]

...

当我的应用程序被选中,我想它播放MP3文件(它应该得到它的路径?)。

我该怎么做?

Example here...

回答

0

你需要你的应用程序与文件扩展名关联。要做到这一点,意图过滤器中添加这两种线和u'r好去

<data android:scheme="file" /> 
<data android:mimeType="*/*"/> 
<data android:pathPattern=".*\\.pdf" /> 

而且你的表现会像这样

<activity name="com.your.activity"> 
<intent-filter> 
    <action android:name="android.intent.action.VIEW" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <category android:name="android.intent.category.BROWSABLE" /> 
    <data android:scheme="file" /> 
    <data android:mimeType="*/*" /> 
    <data android:pathPattern=".*\\.txt" /> 
</intent-filter> 

<data android:scheme="file" /> => this define that the file must be local, not from http or else 
<data android:mimeType="*/*" /> => match any mime type 
<data android:pathPattern=".*\\.txt" /> => this is where you specify what extension you want to match 

更新:要打开这些文件,您需要在活动中为您的onCreate添加如下内容:

Intent intent = getIntent(); 
Action action = intent.getAction(); 
if (action.equals("com.google.android.apps.drive.DRIVE_OPEN")) { 
    // Handle the intent from google drive... 
} else if (action.equals("android.intent.action.VIEW")) { 
    // Handle the intent to view the file... 
} else // And so on... 

更改action.equals如果您的目的。

+0

更新我的答案,你可以使用intent.getData()从该特定意图获取数据 – 2014-11-06 14:29:26

+0

感谢您的帮助。还有一件事我需要做的是我需要在MainActivity中提取mp3路径。你可以帮我吗。 – 2014-11-06 14:30:45

+0

我不确定但试试这个uri = intent.getStringExtra(“URI”) – 2014-11-06 14:32:55

1

您需要简单地添加一个intent filter

补充一点:

<intent-filter> 
     <action android:name="android.intent.action.VIEW" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:scheme="content"/> 
     <data android:scheme="file"/> 
     <data android:mimeType="audio/*"/> 
    </intent-filter> 
+0

谢谢,但这只能解决我的问题的第一部分 需要帮助第二部分 – 2014-11-06 16:24:05

相关问题