我试图给我的应用程序设置为默认音乐播放器的可能性,但我发现了两个问题。
,第一个是关于宣言宣言的:根据Google (清单),我选择的活动的代码是:
<activity
android:name=".Dashboard"
android:theme="@style/NoActionBar"
android:screenOrientation="portrait"
android:launchMode="singleTask">
<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="content"/>
<data android:host="media"/>
<data android:mimeType="audio/*"/>
<data android:mimeType="application/ogg"/>
<data android:mimeType="application/x-ogg"/>
<data android:mimeType="application/itunes"/>
</intent-filter>
</activity>但是如果我试图打开一个音乐文件,我的应用程序就不在兼容应用程序列表中(,为什么?)。因此,我试着用这种方式对文件扩展名进行详细说明:
<activity
android:name=".Dashboard"
android:theme="@style/NoActionBar"
android:screenOrientation="portrait"
android:launchMode="singleTask">
<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="content"/>
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.mp3" />
<data android:host="*" />
</intent-filter>
</activity>这段代码适用于我,但我认为这不是最好的方法。还有一个有一些重要内容的活动--过滤器,我不知道这是否是问题所在。这是第一个活动(所以是启动活动,一个登录活动)。他的第一份宣言是
<activity
android:name=".LoginActivity"
android:label="@string/app_name"
android:theme="@style/NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.MUSIC_PLAYER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.APP_MUSIC" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>在这个时刻,我可以在启动.mp3文件时将我的应用程序设置为默认设置,但是这里是第二个问题:当我的应用程序处于后台时,我无法从Intent拦截Uri。我试着把getIntent().getData()放在OnCreate, OnResume and OnStart上,但是当音乐文件被剪裁并且我的应用程序在后台时,数据总是null。当我的应用程序不在后台时,它是非空的。在哪里/我有什么要解决的?
发布于 2017-08-25 15:02:32
我现在可以回答我自己的问题了。感谢CommonsWare的评论,感谢他对另一个问题的旧回答:
关于清单,这行对我很有用,只需用以下方法更改我的实际意图-过滤器:
<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>关于应用程序在后台时未被截获的数据,我用这样的方式解决了:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*...*/
interceptExternalIntentAndPlaySongFromUri(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
interceptExternalIntentAndPlaySongFromUri(intent);
}
private boolean interceptExternalIntentAndPlaySongFromUri(Intent intent){
Uri data = intent.getData();
if (data != null && intent.getAction() != null &&
intent.getAction().equals(Intent.ACTION_VIEW)){
String scheme = data.getScheme();
String filename = "";
if ("file".equals(scheme)) {
filename = data.getPath();
} else {
filename = data.toString();
}
player.setDataSource(filename);
setIntent(new Intent()); //this is to remove the last intent
//without the above line, last request is reloaded when the user open another app
//and return in this
return true; //has intercepted an external intent
}
return false; //has not intent to intercept
}谢谢@CommonsWare。
https://stackoverflow.com/questions/45879849
复制相似问题