11 java audio android android-intent
我正在创建一个音乐播放器应用程序,我想将该应用程序注册为音乐播放器.因此,当用户点击要播放的任何歌曲时,我的应用程序应被建议为音乐播放器之一.我按照这个指南.
在manifest.xml中添加必要的代码后,它只在建议中显示我的应用程序.当我选择我的应用程序时,什么也没有播放.我认为在主要活动中应该有类似接收器的东西来接收播放所选歌曲的媒体路径.但我不知道如何实现这一点.
我已将以下代码添加到manifest.xml中(供参考):
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file"/>
<data android:mimeType="audio/*"/>
<data android:mimeType="application/ogg"/>
<data android:mimeType="application/x-ogg"/>
<data android:mimeType="application/itunes"/>
</intent-filter>
<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="http" />
<data android:mimeType="audio/*"/>
<data android:mimeType="application/ogg"/>
<data android:mimeType="application/x-ogg"/>
<data android:mimeType="application/itunes"/>
</intent-filter>
<intent-filter
android:priority="-1">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="content" />
<data android:mimeType="audio/*"/>
<data android:mimeType="application/ogg"/>
<data android:mimeType="application/x-ogg"/>
<data android:mimeType="application/itunes"/>
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
添加 IntentFilters 后,使用 getIntent() 从中获取数据
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(@NonNull final Intent intent) {
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
try {
IntentHandler.onActionView(intent);
} catch (IOException e) {
// Handle error
}
}
}
Run Code Online (Sandbox Code Playgroud)
IntentHandler.java
final class IntentHandler {
private static final String TAG = "IntentHandler";
private IntentHandler() {
}
static void onActionView(@NonNull final Intent intent) throws IOException {
final Uri data = intent.getData();
if (data == null) {
Log.w(TAG, "Intent data is null");
throw new IOException("Intent data is null");
}
final String scheme = data.getScheme();
if (scheme == null) {
Log.w(TAG, "Uri scheme is null");
throw new IOException("Uri scheme is null");
}
switch (scheme) {
case "file":
playFile(data);
break;
case "content":
playContent(data);
break;
case "http":
playHttp(data);
break;
default:
Log.w(TAG, "Unhandled Uri scheme: " + scheme);
throw new IOException("Unhandled Uri scheme: " + scheme);
}
}
private static void playFile(@NonNull final Uri data) throws IOException {
Log.d(TAG, "playFile() path = " + data.getPath());
// Play a plain file
}
private static void playContent(@NonNull final Uri data) throws IOException {
Log.d(TAG, "playContent() " + data);
// Use ContentResovler to query the Uri
}
private static void playHttp(@NonNull final Uri data) throws IOException {
Log.d(TAG, "playHttp() " + data);
// Play from network
}
}
Run Code Online (Sandbox Code Playgroud)
如果您不支持从 ContentProvider 或网络播放,请相应地从 AndroidManifest.xml 中删除“http”或“内容”方案的 IntentFilter
编辑handleIntent():如果您不希望您的代码将来看起来很难看,您可以在方法中使用 switch-case 语句而不是一堆“if” 。代码应该如下所示:
private void handleIntent(@NonNull final Intent intent) {
switch (intent.getAction())
{
case Intent.ACTION_VIEW:
{
try {
IntentHandler.onActionView(intent);
} catch (IOException e) {
// Handle error
}
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1144 次 |
| 最近记录: |