import java.io.File; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Intent; import android.media.MediaRecorder; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.provider.MediaStore.MediaColumns; import android.util.Log; import android.view.View; import android.widget.Button; /** * This is main class which shows how to capture/record audio * @author The Developer's Info * */ public class Main extends Activity { private MediaRecorder mediaRecorder; private File file = null; static final String PREFIX = "record"; static final String EXTENSION = ".3gpp"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mediaRecorder = new MediaRecorder(); Button startRecording = (Button) findViewById(R.id.startBtn); Button stopRecording = (Button) findViewById(R.id.stopBtn); startRecording.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { startRecording(); } catch (Exception e) { e.printStackTrace(); } } }); stopRecording.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { stopRecording(); saveToDB(); } }); } /** * This method starts recording process * @throws Exception */ private void startRecording() throws Exception { mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); if (file == null) { File rootDir = Environment.getExternalStorageDirectory(); file = File.createTempFile(PREFIX, EXTENSION, rootDir); } mediaRecorder.setOutputFile(file.getAbsolutePath()); mediaRecorder.prepare(); mediaRecorder.start(); } /** * This method stops recording */ private void stopRecording() { mediaRecorder.stop(); mediaRecorder.release(); } /** * This method sets all metadata for audio file */ private void saveToDB() { ContentValues values = new ContentValues(3); long current = System.currentTimeMillis(); values.put(MediaColumns.TITLE, "My Audio record"); values.put(MediaColumns.DATE_ADDED, (int) (current / 1000)); values.put(MediaColumns.MIME_TYPE, "audio/mp3"); values.put(MediaColumns.DATA, file.getAbsolutePath()); ContentResolver contentResolver = getContentResolver(); Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Uri newUri = contentResolver.insert(base, values); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri)); } }
Dynamically add intents
If there are potentially multiple activities that are relevant to your current Activity or selected item, then the application can dynamically add menu items that execute other services.
During menu creation, define an Intent with the category Intent.ALTERNATIVE_CATEGORY and/or Intent.SELECTED_ALTERNATIVE, the MIME type currently selected (if any), and any other requirements, the same way as you would satisfy an intent filter to open a new Activity. Then call addIntentOptions() to have Android search for any services meeting those requirements and add them to the menu for you. If there are no applications installed that satisfy the Intent, then no additional menu items are added.
Note: SELECTED_ALTERNATIVE is used to handle the currently selected element on the screen. So, it should only be used when creating a Menu in onCreateContextMenu() or onPrepareOptionsMenu(), which is called every time the Options Menu is opened.
Here's an example demonstrating how an application would search for additional services to display on its menu.
public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); // Create an Intent that describes the requirements to fulfill, to be included // in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE. Intent intent = new Intent(null, getIntent().getData()); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); // Search for, and populate the menu with, acceptable offering applications. menu.addIntentOptions( thisClass.INTENT_OPTIONS, // Menu group 0, // Unique item ID (none) 0, // Order for the items (none) this.getComponentName(), // The current Activity name null, // Specific items to place first (none) intent, // Intent created above that describes our requirements 0, // Additional flags to control items (none) null); // Array of MenuItems that corrolate to specific items (none) return true; }
For each Activity found that provides an Intent Filter matching the Intent defined, a menu item will be added, using the android:label value of the intent filter as the text for the menu item. The addIntentOptions() method will also return the number of menu items added.
Also be aware that, when addIntentOptions() is called, it will override any and all menu items in the menu group specified in the first argument.
If you wish to offer the services of your Activity to other application menus, then you only need to define an intent filter as usual. Just be sure to include the ALTERNATIVE and/or SELECTED_ALTERNATIVE values in the name attribute of a <category> element in the intent filter. For example:
<intent-filter label="Resize Image"> ... <category android:name="android.intent.category.ALTERNATIVE" /> <category android:name="android.intent.category.SELECTED_ALTERNATIVE" /> ... </intent-filter>
read more about writing intent filters in the Intents and Intent Filters document.
For a sample application using this technique, see the Note Pad sample code.
public class HTMLListActivity extends ListActivity {
private List<String> filesEntries = new ArrayList<String>();
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
AppUtils.showDebug(getApplicationContext(), "HTMLListActivity - onCreate!!");
filesEntries.add("a1_bienvenida");
filesEntries.add("a2_info_tabaquismo");
filesEntries.add("a3_tests");
filesEntries.add("diez_mandamientos");
filesEntries.add("test");
filesEntries.add("mitos_del_tabaco");
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
R.layout.cigar_row, this.filesEntries);
this.setListAdapter(directoryList);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
TextView tv = (TextView)v;
Intent i = new Intent( getApplicationContext(), HTMLViewer.class );
i.putExtra( "fileName", tv.getText());
startActivity( i );
}
}
只要使用filesEntries list泛型就可以了