0
 public class MainActivity extends Activity {
 private static final String TAG = "MainActivity";
private MusicIntentReceiver myReceiver;
Context context = MainActivity.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myReceiver = new MusicIntentReceiver();
}

@Override
public void onResume() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(myReceiver, filter);
    super.onResume();
}

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");
                Toast.makeText(MainActivity.this, "Headset is unplugged", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(MainActivity.this, "Headset is plugged", Toast.LENGTH_SHORT).show();
                 PackageManager pm = getPackageManager();
                /*List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

                for (ApplicationInfo packageInfo : packages) {
                    Toast.makeText(MainActivity.this,"Installed package :" + packageInfo.packageName, Toast.LENGTH_SHORT).show();
                    Toast.makeText(MainActivity.this, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName), Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "Installed package :" + packageInfo.packageName);
                    Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); 
                }*/
                Intent intent1 = new Intent(Intent.ACTION_VIEW);
                Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"1"); 
                intent1.setData(uri);
                List<ResolveInfo> playerList ;
                playerList = pm.queryIntentActivities(intent1, 0);

                if( playerList != null){
                    for (int i = 0 ; i < playerList.size() ; i++){
                        //playerList.get(i).loadIcon(getPackageManager());
                        Log.v(TAG, playerList.size()+"");
                        Toast.makeText(MainActivity.this,playerList.get(i).loadLabel(pm)+":"+playerList.get(i).loadIcon(pm), Toast.LENGTH_SHORT).show();

                    }
                }

                break;
            default:
                Log.d(TAG, "I have no idea what the headset state is");
                Toast.makeText(MainActivity.this, "I have no idea what the headset state is", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

@Override
public void onPause() {
    unregisterReceiver(myReceiver);
    super.onPause();
}

}

Actually i want to load all the apps installed in the phone and show a dialog to the user whenever user plugs in the headset I am using the above mentioned code but always loading the BROWSER application in android even though not loading the media player My phone is Micromax canvas

  • And the problem is..? – Michael Sep 25 '13 at 12:30
  • http://stackoverflow.com/questions/10867957/android-playing-a-song-file-using-default-music-player – Auto-Droid ツ Sep 25 '13 at 12:33
  • Many applications don't support *both* audio and video. But you can create two lists and create the intersection of both: http://stackoverflow.com/questions/5283047/intersection-union-of-arraylists-in-java – zapl Sep 25 '13 at 12:35
  • Pls edit your question and let us know your exact problem bcoz im not get the link of url and your question – Auto-Droid ツ Sep 25 '13 at 12:41
  • https://xjaphx.wordpress.com/2011/06/12/create-application-launcher-as-a-list/ see the link it may help but the it will show the list of all the application installed in the phone – Auto-Droid ツ Sep 25 '13 at 13:11

1 Answers1

0

If you want to open an audio or video recording then this is the answer

 File filenew = new File(Environment.getExternalStorageDirectory(), "/Recording/"+filename);
               int file_size = Integer.parseInt(String.valueOf(filenew.length()/1024));
              // Toast.makeText(getApplicationContext(), ""+file_size, 10000).show();
               Intent intent = new Intent();  
              intent.setAction(android.content.Intent.ACTION_VIEW);  

              intent.setDataAndType(Uri.fromFile(filenew), "audio/*");  
              startActivity(intent);
Auto-Droid ツ
  • 1,583
  • 1
  • 17
  • 31