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

4

1 回答 1

0

如果您想打开音频或视频记录,那么这就是答案

 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);
于 2013-09-25T12:37:03.483 回答