Android SoundRecorder (recorder) adds a recording list menu item

Source: Internet
Author: User

Android SoundRecorder (recorder) adds a recording list menu item

Add a recording list item to the recorder. Click it to view the existing recording file.

As follows:

 

 

The code to be modified is as follows:

 

The original recorder does not have the recording list option, so we need to add it.

SoundRecorder esmenumain_menu.xml add recording menu options

 

 

Android: id = @ + id/menu_item_filetype

Android: title = @ string/format_setting/>

Android: id = @ + id/menu_item_storage

Android: title = @ string/storage_setting/>

Android: id = @ + id/menu_item_keyboard

Android: title = @ string/keyboard/>

Android: id = @ + id/menu_item_list

Android: title = @ string/audio_db_playlist_name/>

 

 

 

SoundRecordersrccomandroidsoundrecorderSoundRecorder. java adds a response to the menu item

 

Public static final String SOUNDRECORDER_PLAYLIST = My recordings;

 

Public boolean onOptionsItemSelected (MenuItem item ){

// TODO Auto-generated method stub

Switch (item. getItemId ()){

Case R. id. menu_item_keyboard:

If (mRecorder. state () = Recorder. IDLE_STATE ){

InputMethodManager inputMgr =

(InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE );

InputMgr. toggleSoftInput (0, 0 );

}

Break;

Case R. id. menu_item_filetype:

If (mRecorder. state () = Recorder. IDLE_STATE ){

OpenOptionDialog (SETTING_TYPE_FILE_TYPE );

}

Break;

Case R. id. menu_item_storage:

If (mRecorder. state () = Recorder. IDLE_STATE ){

OpenOptionDialog (SETTING_TYPE_STORAGE_LOCATION );

}

Break;

Case R. id. menu_item_list:

Intent intent = new Intent (this, RecordingList. class );

StartActivity (intent );

}

Return super. onOptionsItemSelected (item );

}

 

 

/SoundRecorder/src/com/android/soundrecorder/RecordingList. java add activity in the display list

 

Package com. android. soundrecorder;

 

Import android. app. ListActivity;

Import android. content. ContentResolver;

Import android. content. Context;

Import android. content. ContentUris;

Import android. content. ContentValues;

Import android. content. Intent;

Import android. database. Cursor;

Import android.net. Uri;

Import android. OS. Bundle;

Import android. util. Log;

Import android. widget. CursorAdapter;

Import android. widget. SimpleCursorAdapter;

Import android. widget. TextView;

Import android. widget. Toast;

Import android. content. res. Resources;

Import android. provider. MediaStore;

Import android. view. View;

Import android. widget. ListView;

Import android. view. ContextMenu;

Import android. view. ContextMenu. ContextMenuInfo;

Import android. widget. AdapterView. AdapterContextMenuInfo;

Import android. view. LayoutInflater;

Import android. view. Menu;

Import android. view. MenuItem;

Import java. io. File;

Import android. content. DialogInterface;

Import android. content. DialogInterface. OnCancelListener;

Import android. content. DialogInterface. OnClickListener;

Import android. app. Dialog;

Import android. app. AlertDialog;

Import android. widget. EditText;

Import android. text. InputFilter;

 

Public class RecordingList extends ListActivity {

Static final String TAG = RecordingList;

 

Private static final int MAX_FILE_NAME_LENGTH = 80;

 

Private ListView mTrackList;

 

Private static final int MENU_RENAME = 0;

Private static final int MENU_DELETE = 1;

 

Private static final int DIALOG_DELETE = 0;

 

Private String mCurrentRecordingTitle;

Private long mSelectedId;

Private String mSelectedName;

Private SimpleCursorAdapter mAdapter;

 

@ Override

Public void onCreate (Bundle icicle ){

Super. onCreate (icicle );

Init ();

MTrackList = getListView ();

MTrackList. setOnCreateContextMenuListener (this );

 

If (icicle! = Null ){

OnRestoreInstanceState (icicle );

}

}

 

@ Override

Protected void onSaveInstanceState (Bundle out ){

Super. onSaveInstanceState (out );

Out. putString (mSelectedName, mSelectedName );

Out. putLong (mSelectedId, mSelectedId );

}

 

Protected void onRestoreInstanceState (Bundle in ){

MSelectedName = in. getString (mSelectedName );

MSelectedId = in. getLong (mSelectedId );

}

 

Public void init (){

 

SetContentView (R. layout. recording_list );

MakeCursor ();

UpdateRecordingList (mCursor); // add by xiaoya 2014/06/17 for [bugId: vk8500213165]

SetTitle (R. string. audio_db_playlist_name );

 

MAdapter = new SimpleCursorAdapter (this,

Android. R. layout. simple_list_item_1, mCursor,

New String [] {MediaStore. Video. Media. DISPLAY_NAME },

New int [] {android. R. id. text1 });

 

SetListAdapter (mAdapter );

}

 

/**

* Add by xiaoya 2014/06/17 for [bugId: vk8500426165]

* @ Param cursor

*/

Private void updateRecordingList (Cursor cursor ){

If (null! = Cursor ){

Cursor. moveToFirst ();

While (cursor. moveToNext ()){

MSelectedName = cursor. getString (cursor

. GetColumnIndexOrThrow (MediaStore. Audio. Media. DATA ));

MSelectedId = cursor. getLong (cursor

. GetColumnIndexOrThrow (MediaStore. Audio. Playlists. Members. AUDIO_ID ));

Try {

File f = new File (mSelectedName );

If (! F. exists ()){

Delete ();

}

} Catch (Exception e ){

Return;

}

}

}

}

 

@ Override

Protected void onListItemClick (ListView l, View v, int position, long id ){

Intent intent = new Intent (Intent. ACTION_VIEW );

MCursor. moveToPosition (position );

String type = mCursor. getString (mCursor

. GetColumnIndexOrThrow (MediaStore. Video. Media. MIME_TYPE ));

MSelectedName = mCursor. getString (mCursor

. GetColumnIndexOrThrow (MediaStore. Audio. Media. DATA ));

// Add by xiaoya 2014/06/11 for [bugId: vk8500385165] begin

Try {

File f = new File (mSelectedName );

If (! F. exists ()){

Toast. makeText (v. getContext (), This file has been removed !, Toast. LENGTH_LONG). show ();

Return;

}

} Catch (Exception e ){

Return;

}

// Add by xiaoya 2014/06/11 for [bugId: vk8500824165] end

Intent. setDataAndType (Uri. fromFile (new File (mSelectedName), type );

 

StartActivity (intent );

}

 

@ Override

Public void onCreateContextMenu (ContextMenu menu, View view,

ContextMenuInfo menuInfoIn ){

Menu. add (0, MENU_DELETE, 0, R. string. menu_delete );

AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfoIn;

MCursor. moveToPosition (mi. position );

Try {

MCurrentRecordingTitle = mCursor. getString (mCursor

. GetColumnIndexOrThrow (MediaStore. Audio. Media. TITLE ));

MSelectedName = mCursor. getString (mCursor

. GetColumnIndexOrThrow (MediaStore. Audio. Media. DATA ));

Int id_idx = mCursor

. GetColumnIndexOrThrow (MediaStore. Audio. Playlists. Members. AUDIO_ID );

MSelectedId = mCursor. getLong (id_idx );

} Catch (IllegalArgumentException ex ){

MSelectedId = mi. id;

}

Menu. setHeaderTitle (mCurrentRecordingTitle );

}

 

@ Override

Public boolean onContextItemSelected (MenuItem item ){

Switch (item. getItemId ()){

Case MENU_DELETE :{

ShowDialog (DIALOG_DELETE );

Return true;

}

}

Return super. onContextItemSelected (item );

}

 

@ Override

Protected Dialog onCreateDialog (int id ){

Switch (id ){

Case DIALOG_DELETE :{

Return new AlertDialog. Builder (this)

. SetTitle (getString (R. string. really_delete ))

. SetIcon (android. R. drawable. ic_dialog_alert)

. SetPositiveButton (android. R. string. OK,

New OnClickListener (){

Public void onClick (DialogInterface dialog,

Int which ){

Delete ();

}

}). SetNegativeButton (android. R. string. cancel, null)

. Create ();

}

}

Return null;

}

 

Private void delete (){

StringBuilder where = new StringBuilder ();

Where. append (MediaStore. Audio. Media. _ ID + IN ();

Where. append (mSelectedId );

Where. append ());

GetContentResolver (). delete (

MediaStore. Audio. Media. EXTERNAL_CONTENT_URI, where. toString (),

Null );

File f = new File (mSelectedName );

Log. e (TAG, delete file + mSelectedName +, mSelectedId:

+ MSelectedId );

If (! F. delete ()){

Log. e (TAG, Failed to delete file + mSelectedName );

}

}

 

Private void makeCursor (){

String [] cols = new String [] {MediaStore. Audio. Playlists. Members. _ ID,

MediaStore. Audio. Media. TITLE, MediaStore. Audio. Media. DATA,

MediaStore. Audio. Media. DISPLAY_NAME,

MediaStore. Audio. Playlists. Members. AUDIO_ID,

MediaStore. Audio. Media. MIME_TYPE ,};

If (mCursor! = Null ){

MCursor. close ();

MCursor = null;

}

ContentResolver resolver = getContentResolver ();

 

If (resolver = null ){

Log. e (TAG, resolver = null );

} Else if (-1 = getPlaylistId ()){

Log. e (TAG, invalid playlist id:-1 );

} Else {

Uri uri = MediaStore. Audio. Playlists. Members. getContentUri (

External, Long. valueOf (getPlaylistId ()));

 

MCursor = resolver. query (uri, cols, null, null, mSortOrder );

}

}

 

Private int getPlaylistId (){

Resources res = getResources ();

Uri uri = MediaStore. Audio. Playlists. getContentUri (external );

Final String [] ids = new String [] {MediaStore. Audio. Playlists. _ ID };

Final String where = MediaStore. Audio. Playlists. NAME + = ?;

Final String [] args = new String [] {

// SoundRecorder. SOUNDRECORDER_PLAYLIST

My recordings

};

Cursor cursor = getContentResolver (). query (uri, ids, where, args, null );

If (cursor = null ){

Log. v (TAG, query returns null );

}

Int id =-1;

If (cursor! = Null ){

Cursor. moveToFirst ();

If (! Cursor. isAfterLast ()){

Id = cursor. getInt (0 );

}

Cursor. close ();

}

Return id;

}

 

@ Override

Public void onDestroy (){

If (mCursor! = Null ){

MCursor. close ();

MCursor = null;

}

Super. onDestroy ();

}

 

Private Cursor mCursor;

Private String mWhereClause;

Private String mSortOrder;

}

 

 

     
  
 

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.