2012-08-01 116 views
0

在我的Android應用程序中,我有一個應打開設備音樂庫的按鈕。 我用下面的代碼現在Android - 正確打開默認音樂庫

Intent intent = new Intent("android.intent.action.MUSIC_PLAYER"); 
startActivity(intent); 

據我瞭解,有可能是沒有默認的音樂庫設備。 所以,我想要的代碼片段將打開音樂庫,如果有可能,並顯示一些消息(或別的東西),否則。

回答

0

將它包圍在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) 
} 
+0

您也可以要求意圖有Intent.CATEGORY_DEFAULT'的'一類,你必須測試這個在設備上。 – 2012-08-01 16:37:09

相關問題