編輯:關於Android編程
在錄音機中添加一個錄音列表項,點擊後用戶可以看到已經存在的錄音文件。
效果圖如下:
修改代碼參照如下:
最初的錄音機是沒有錄音列表選項的,所以我們要加上去。
SoundRecorder esmenumain_menu.xml添加錄音菜單選項
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 添加對菜單項的響應
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添加展示列表的activity
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:VK85000000165]
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:VK85000000165]
* @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:VK85000000165] 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:VK85000000165] 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;
}
應用項目需要要屏蔽HOME鍵。項目本身的要求是讓按下HOME鍵後程序不做任何響應,就像按下返回鍵一樣在onBackPressed 方法中直接return啥都
1 秒殺業務分析正常電子商務流程(1)查詢商品;(2)創建訂單;(3)扣減庫存;(4)更新訂單;(5)付款;(6)賣家發貨秒殺業務的特性(1)低廉價格;(2)大幅推廣;(
1、android支持庫未安裝 編譯不過,提示如下: Could not find any version that matches com.android.suppor
BroadcastReceiver除了接收用戶所發送的廣播消息之外,還有一個重要的用途:接收系統廣播。如果應用需要在系統特定時刻執行某些操作,就
這個錯誤翻譯的意思是:不能在沒有Looper.prepare的線程裡面創