0

Im trying to make an app that automatically launches the music player of your choice when the headphones are plugged in. For this to work, when the user plugs in their headphones I want an intent to show all the applications on their phone so they can click there default music player such as rhapsody, spotify, etc. Since everybody has different music apps. I can't use an intent to launch just the package name because it might not be the same for everybody. Is there an intent to show all applications so the user can pick their default one. Thanks.

Another way i was thinking would be to search the specific third party app in a list view on the phone and then set it as default.

code I am trying to use, code needs to work inside case1

private class MusicIntentReceiver extends BroadcastReceiver {
    @Override public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int state = intent.getIntExtra("state", -1);
            switch (state) {
                case 0:
                    Log.d(TAG, "Headset is unplugged");
                    break;
                case 1:
                    final PackageManager pm = getPackageManager();
                    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
                    for (ApplicationInfo packageInfo : packages) {
                        Log.d(TAG, "Installed package :" + packageInfo.packageName);
                        Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
                        Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
                    }
                    break;
                default:
                    Log.d(TAG, "I have no idea what the headset state is");
            }
        }
    }
}
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Jordan
  • 407
  • 5
  • 20

1 Answers1

0

You can query the System

public static boolean isSupportedIntent(@NonNull final Intent intent, @NonNull @ApplicationContext final Context applicationContext) {
    Preconditions.checkNotNull(intent);
    Preconditions.checkNotNull(applicationContext);
    //
    final List<ResolveInfo> list = applicationContext.getPackageManager().queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    return !list.isEmpty();
}

So, instead of returning boolean there you can return the ResolveInfo to get further infos

Kitesurfer
  • 3,438
  • 2
  • 29
  • 47
  • I'm not exactly sure how to implement that into my app. I added some code that shows what im trying to accomplish – Jordan Oct 12 '14 at 16:24
  • You need to query for application that can handle some kind of intent for data like audio/mpeg3 and check what you receive from the query. – Kitesurfer Oct 12 '14 at 17:28