在我的Android应用程序中,我有一个按钮,可以打开设备的音乐库。我现在使用以下代码
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(intent);我知道有些设备可能没有默认的音乐库。所以,我想要的代码片段将打开音乐库,如果它是可能的,并显示一些消息(或其他东西),否则。
发布于 2012-08-02 00:35:43
将其包含在try/catch块中。如果抛出ActivityNotFoundException,则设备上没有能够接收该意图的组件:
try {
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(intent);
catch (ActivityNotFoundException anfe) {
// Handle no component found here (e.g. show a toast or dialog)
}https://stackoverflow.com/questions/11763511
复制相似问题