編輯:關於Android編程
在oncreate 中執行:
復制代碼 代碼如下:
public void onCreate() {
super.onCreate();
Log.i("TAG", "服務啟動了");
// 對電話的來電狀態進行監聽
TelephonyManager telManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
// 注冊一個監聽器對電話狀態進行監聽
telManager.listen(new MyPhoneStateListener(),
PhoneStateListener.LISTEN_CALL_STATE);
}
實現MyPhoneStateListener:
復制代碼 代碼如下:
private class MyPhoneStateListener extends PhoneStateListener {
MediaRecorder recorder;
File audioFile;
String phoneNumber;
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: /* 無任何狀態時 */
if (recorder != null) {
recorder.stop(); //停止刻錄
recorder.reset(); //重設
recorder.release(); //刻錄完成一定要釋放資源
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起電話時 */
try {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 設置音頻采集原
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //內容輸出格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //音頻編碼方式
// recorder.setOutputFile("/sdcard/myvoice.amr");
audioFile = new File(
Environment.getExternalStorageDirectory(),
phoneNumber + "_" + System.currentTimeMillis()
+ ".3gp");
recorder.setOutputFile(audioFile.getAbsolutePath());
Log.i("TAG", audioFile.getAbsolutePath());
recorder.prepare(); //預期准備
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
case TelephonyManager.CALL_STATE_RINGING: /* 電話進來時 */
phoneNumber = incomingNumber;
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
通過上續兩個對應的步驟即可以實現通過server對電話的監聽工作,在CALL_STATE_IDLE無狀態(即空閒狀態)、CALL_STATE_OFFHOOK 接通電話(即掛起)、CALL_STATE_RINGING 電話進來(即來電時)這幾個狀態中執行。
附:Android拍照、錄像、錄音代碼范例
復制代碼 代碼如下:
package com.cons.dcg.collect;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
import android.app.*;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.*;
import android.widget.*;
public class RecordActivity extends Activity implements OnClickListener {
private static final int RESULT_CAPTURE_IMAGE = 1;// 照相的requestCode
private static final int REQUEST_CODE_TAKE_VIDEO = 2;// 攝像的照相的requestCode
private static final int RESULT_CAPTURE_RECORDER_SOUND = 3;// 錄音的requestCode
private String strImgPath = "";// 照片文件絕對路徑
private String strVideoPath = "";// 視頻文件的絕對路徑
private String strRecorderPath = "";// 錄音文件的絕對路徑
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.problem_report);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_CAPTURE_IMAGE://拍照
if (resultCode == RESULT_OK) {
Toast.makeText(this, strImgPath, Toast.LENGTH_SHORT).show();
}
break;
case REQUEST_CODE_TAKE_VIDEO://拍攝視頻
if (resultCode == RESULT_OK) {
Uri uriVideo = data.getData();
Cursor cursor=this.getContentResolver().query(uriVideo, null, null, null, null);
if (cursor.moveToNext()) {
/** _data:文件的絕對路徑 ,_display_name:文件名 */
strVideoPath = cursor.getString(cursor.getColumnIndex("_data"));
Toast.makeText(this, strVideoPath, Toast.LENGTH_SHORT).show();
}
}
break;
case RESULT_CAPTURE_RECORDER_SOUND://錄音
if (resultCode == RESULT_OK) {
Uri uriRecorder = data.getData();
Cursor cursor=this.getContentResolver().query(uriRecorder, null, null, null, null);
if (cursor.moveToNext()) {
/** _data:文件的絕對路徑 ,_display_name:文件名 */
strRecorderPath = cursor.getString(cursor.getColumnIndex("_data"));
Toast.makeText(this, strRecorderPath, Toast.LENGTH_SHORT).show();
}
}
break;
}
}
/**
* 照相功能
*/
private void cameraMethod() {
Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
strImgPath = Environment.getExternalStorageDirectory().toString() + "/CONSDCGMPIC/";//存放照片的文件夾
String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";//照片命名
File out = new File(strImgPath);
if (!out.exists()) {
out.mkdirs();
}
out = new File(strImgPath, fileName);
strImgPath = strImgPath + fileName;//該照片的絕對路徑
Uri uri = Uri.fromFile(out);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(imageCaptureIntent, RESULT_CAPTURE_IMAGE);
}
/**
* 拍攝視頻
*/
private void videoMethod() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
}
/**
* 錄音功能
*/
private void soundRecorderMethod() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/amr");
startActivityForResult(intent, RESULT_CAPTURE_RECORDER_SOUND);
}
/**
* 提示信息
* @param text
* @param duration
*/
private void showToast(String text, int duration) {
Toast.makeText(ProblemReport.this, text, duration).show();
}
}
安裝完android studio的初衷是為了看一個項目的源碼,因為現在的公司還在用elipse開發。。。不過,我發現,android studio還是巨好用的。就這幾天
Android4.4 fence機制分析 在任何一個系統中,無可避免的都會跟各種buffers打交道,最經典的模式就是消費-生產者模式,一個獨立的buffer在它們之間
0x00閱讀本文前,建議讀者首先閱讀Android加殼原理,參考文章Android中的Apk的加固(加殼)原理解析和實現。如果沒有看過這篇文章,本文理解起來比較困難。0x
1. 默認為debug mode,使用的簽名文件在: $HOME/.android/debug.keystore 2. Release Mode 簽