2012-12-31 41 views
1

我試圖從服務中啓動MediaPlayer,並且它不像預期的那樣打開。我米收到以下異常,Android:通過服務啓動音樂播放器

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MUSIC_PLAYER flg=0x10000000 } 

請找到該獲取的服務調用的代碼片斷,

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 

Android清單

<service android:name="com.lakshmi.shakenfun.AlertService" > 
    <intent-filter > 
     <action android:name="android.intent.action.MUSIC_PLAYER" /> 
    </intent-filter> 
</service> 

請不要讓我知道,在這裏我做錯了。

我的目標平臺是8個

感謝, 拉梅什

+3

您無法使用'startActivity(intent);'啓動'服務'。要使用'startService(...)'啓動一個'Service'。 – Squonk

+0

您是否嘗試啓動設備的媒體播放器?如果是這樣,爲什麼你在自己的服務的''上定義了'MEDIA_PLAYER'動作?關於錯誤,顯然你的設備或模擬器上沒有支持特定「Intent」動作的活動。 – CommonsWare

+0

我需要從服務中啓動媒體播放器。我不想開始一項服務。我提到這個鏈接,從我的服務啓動媒體播放器, http://stackoverflow.com/questions/3114471/android-launching-music-player-using-intent 要只需啓動音樂播放器做: Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER); startActivity(intent); –

回答

1

MediaStore.INTENT_ACTION_MUSIC_PLAYER從API 15新代碼棄用是:

try { 
    String pkgname = "com.sec.android.app.music"; 
    PackageManager pkgmanager = getPackageManager(); 
    Intent intent = pkgmanager.getLaunchIntentForPackage(pkgname); 
    startActivity(intent); 
} catch(Exception e) { 
    // music player not found 
} 

只需在您的按鈕的監聽器添加的代碼,它會調用默認的音樂播放器。