2014-11-06 207 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

相關問題