編輯:Android開發實例
Android開發教程,Android開發當中傳感器的使用也是常常遇到的,今天我們來看看IBM的安卓開發代碼是在怎麼寫的吧,主要代碼如下:
IBMAudio.java代碼:
/*
*
* IBMAudio.java
* IBM的Developerworks示例代碼
* Author: anonymity
*
*/
package com.msi.ibm.audio;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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.util.Log;
import android.view.View;
import android.widget.Button;
public class IBMAudio extends Activity {
public MediaRecorder mrec = null;
private Button startRecording = null;
private Button stopRecording = null;
private static final String TAG = "SoundRecordingDemo";
File audiofile;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mrec = new MediaRecorder();
startRecording = (Button)findViewById(R.id.startrecording);
stopRecording = (Button)findViewById(R.id.stoprecording);
startRecording.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
try
{
startRecording.setEnabled(false);
stopRecording.setEnabled(true);
stopRecording.requestFocus();
startRecording();
}catch (Exception ee)
{
Log.e(TAG,"Caught io exception " + ee.getMessage());
}
}
});
stopRecording.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
startRecording.setEnabled(true);
stopRecording.setEnabled(false);
startRecording.requestFocus();
stopRecording();
processaudiofile();
}
});
stopRecording.setEnabled(false);
startRecording.setEnabled(true);
}
protected void processaudiofile() {
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
// this does not always seem to work cleanly....
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
}
protected void startRecording() throws IOException
{
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// mRecorder.setOutputFile("/sdcard/yousuck2.3gp");
if (audiofile == null) {
File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("ibm", ".3gp", sampleDir);
}
catch (IOException e)
{
Log.e(TAG,"sdcard access error");
return;
}
}
mrec.setOutputFile(audiofile.getAbsolutePath());
mrec.prepare();
mrec.start();
}
protected void stopRecording() {
mrec.stop();
mrec.release();
}
}
R.java代碼:
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.msi.ibm.audio;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int startrecording=0x7f050000;
public static final int stoprecording=0x7f050001;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
大家在進行其他語言的編程時應該都見過甚至用過標簽組件,Android開發中也經
Android SDK提供了很多示例程序,從這些示例代碼的閱讀和試驗中能夠學習
JSON代表JavaScript對象符號。它是一個獨立的數據交換格式,是XML的最佳替代品。本章介紹了如何解析JSON文件,並從中提取所需的信息。Android提供了四個
今天繼續講解Fragment組件的特性,主要是跟Activity的交互和生命周期的關系,我們前面已經說過Fragment是依賴於Activity的,而且生命周期也