Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

android - Open music player on Galaxy S3

This following code works on any android device (api >= 8) but not on Samsung Galaxy S3 (music player not found).

try {
    String name;
    try {
        name = (String) MediaStore.class.getDeclaredField("INTENT_ACTION_MUSIC_PLAYER").get(null);
    } catch(Exception e) {
        name = (String) Intent.class.getDeclaredField("CATEGORY_APP_MUSIC").get(null);
    }
    startActivity(new Intent(name));
} catch(Exception e) {
    // music player not found
}

Samsung constructor layer is something to it? Does anyone have a solution to open the media player on a Galaxy S3?

As an update, I have changed my mistake but I still no get mediaplayer on Galaxy S3:

try {
    try {
        // 8 <= API < 15
        String action = (String) MediaStore.class.getDeclaredField("INTENT_ACTION_MUSIC_PLAYER").get(null);
        Intent intent = new Intent(action);
        startActivity(intent);
    } catch(Exception e) {
        // 15 <= API
        String category = (String) Intent.class.getDeclaredField("CATEGORY_APP_MUSIC").get(null);
        Method method = Intent.class.getMethod("makeMainSelectorActivity", String.class, String.class);
        Intent intent = (Intent) method.invoke(null, Intent.ACTION_MAIN, category);
        startActivity(intent);
    }
catch(Exception e) {
    // music player not found
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

CATEGORY_APP_MUSIC is a category. opening an intent directly on this won't work.

According to the documentation :

public static final String CATEGORY_APP_MUSIC

Since: API Level 15 Used with ACTION_MAIN to launch the music application. The activity should be able to play, browse, or manipulate music files stored on the device.

NOTE: This should not be used as the primary key of an Intent, since it will not result in the app launching with the correct action and category. Instead, use this with makeMainSelectorActivity(String, String) to generate a main Intent with this category in the selector

you need to open it using action MAIN and setting the category as CATEGORY_APP_MUSIC


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...