Android : Launch Music Player via Service

两盒软妹~` 提交于 2019-12-24 07:58:04

问题


I am trying to launch the MediaPlayer from a service, and its not wroking as expected. I m getting the following exception,

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

Please find the snippet of code that gets invoked in the service,

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

Android Manifest

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

Please do let me know, where I am doing wrong.

My target platform is 8

Thanks, Ramesh


回答1:


Perhaps your Target Platform of 8 is too low for that api? Do you have this music player loaded? https://play.google.com/store/apps/details?id=com.google.android.music&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5nb29nbGUuYW5kcm9pZC5tdXNpYyJd




回答2:


MediaStore.INTENT_ACTION_MUSIC_PLAYER is deprecated from API 15 the new code is:

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
}

Just add this code on your button listener and it will call the default music player.



来源:https://stackoverflow.com/questions/14106494/android-launch-music-player-via-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!