編輯:關於Android編程
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); TelephonyManager mTelephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); mTelephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE); ITelephony mTelephony = ITelephony.Stub.asInterface(ServiceManager.getService("phone")); } class PhoneListener extends PhoneStateListener { private MediaRecorder mRecorder = null; private boolean flag = false; private String phoneNum = null; @Override public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub switch (state) { case TelephonyManager.CALL_STATE_IDLE: try { if (mRecorder != null) { mRecorder.stop(); // 停止 mRecorder.release();// 釋放 mRecorder = null;// 垃圾回收 } if (flag) { mTelephony.call(phoneNum); flag = false; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case TelephonyManager.CALL_STATE_OFFHOOK: //通話錄音 try { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 此處只實現了錄本地MIC輸入的聲音,若想錄入對立通話者的聲音 mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setOutputFile("/mnt/sdcard/" + incomingNumber + "_"+ System.currentTimeMillis() + ".3gp"); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mRecorder.prepare(); } catch (Exception e) { e.printStackTrace(); } mRecorder.start(); Thread.sleep(30 * 1000); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case TelephonyManager.CALL_STATE_RINGING: //來電自動接聽 try { phoneNum = incomingNumber; flag = true; if (mTelephony.isRinging()) { mTelephony.silenceRinger(); mTelephony.answerRingingCall(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } break; default: break; } super.onCallStateChanged(state, incomingNumber); } } }
懷著無比崇敬的心情翻開了這本書,路漫漫其修遠兮,程序人生,為自己加油!一.序作為這本書的第一章,主席還是把Activity搬上來了,也確實,和Activity打交道的次數
方向傳感器 Orientation (現在幾乎已經不用了,因為其實不存在真正的方向) 磁場傳感器 Magnetic Field 溫度傳感器 Temperatu
本項目圖片素材均來自今日頭條,QQ側滑沒有使用Android原生的NavigationDrawer,而使用的是第三方SlidingMenu,原因是這個控件暫時沒有仔細研究
序言:Activity作為Android四大組件之一,其重要性不言而喻。本文目錄結構:1.Acticity是什麼2.Activity的創建3.Activity的生命周期4