Android get list of installed music players

foreword

We usually wear headphones to listen to music during exercise, so when running in the pedometer, we naturally need to turn on our favorite music player. For convenience, I added a button in the pedometer to call the installed music player , but no matter how I searched, I couldn’t find the APP that can be opened, but there is a music player on my mobile phone. . After tossing for a long time, I only found the music player that comes with the system, and I couldn't find anything else. It wasn't until I saw the <queries> tag that I realized it.

accomplish

In the AndroidManifest.xml file

...
<queries>
        <intent>
            <action android:name="android.intent.action.VIEW"/>
            <data android:scheme="audio/*"/>
        </intent>
        <intent>
            <action android:name="android.intent.action.MEDIA_BUTTON"/>
        </intent>
</queries>
<application
...

Implementation:

    private void getMedia(){
        String TAG = "music";
        Log.i(TAG, "test: ");
        // 创建一个 Intent 对象,设置 action 为 ACTION_VIEW,type 为 audio/*
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("file://"),"audio/mp3");
        // 查询所有可以处理该 Intent 的应用程序
        PackageManager pm = getPackageManager();
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
        List<String> apps = new ArrayList<>();
        // 遍历查询结果,获取所有可以处理音频的应用程序的信息
        for (ResolveInfo resolveInfo : resolveInfos) {
            String packageName = resolveInfo.activityInfo.packageName;
            String className = resolveInfo.activityInfo.name;
            String label = resolveInfo.loadLabel(pm).toString();
            // 处理查询结果
            Log.i(TAG, "packageName: "+packageName);
            Log.i(TAG, "className: "+className);
            Log.i(TAG, "label: "+label);
            apps.add(label);
        }
    }

As you can see, after adding the content in the <query> tag, you can get it

Guess you like

Origin blog.csdn.net/TDSSS/article/details/129706770