1創(chuàng)建Service
package com.shiliu.callrecording;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.media.FaceDetector;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Binder;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import com.vondear.rxtool.view.RxToast;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static android.support.v4.app.NotificationCompat.PRIORITY_MAX;
/**
* Created by Easzz on 2015/12/6.
*/
public class RecorderServiceextends Service {
private MediaRecorderrecorder;//錄音的一個(gè)實(shí)例
? ? //通過(guò)binder實(shí)現(xiàn)調(diào)用者client與Service之間的通信
? ? private NotificationCompat.Builderbuilder;
private NotificationManagernotificationManager;
private boolean isFirst =true;
public RecorderService() {
}
@Override
? ? public void onCreate() {
super.onCreate();
//如果API在26以上即版本為O則調(diào)用startForefround()方法啟動(dòng)服務(wù)
//? ? ? ? if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
? ? ? ? setForegroundService();
//? ? ? ? }else {
//
//? ? ? ? }
? ? }
/**
? ? * 通過(guò)通知啟動(dòng)服務(wù)
? ? */
? ? public void setForegroundService() {
//設(shè)定的通知渠道名稱(chēng)
? ? ? ? String channelName = getString(R.string.channel_name);
final String CHANNEL_ID ="com.appname.notification.channel";
//設(shè)置通知的重要程度
? ? ? ? int importance = NotificationManager.IMPORTANCE_LOW;
Intent mIntent =new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, mIntent,0);
//在創(chuàng)建的通知渠道上發(fā)送通知
? ? ? ? builder =new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(R.mipmap.ic_launcher)//設(shè)置通知圖標(biāo)
? ? ? ? ? ? ? ? .setContentTitle("電話監(jiān)聽(tīng)")//設(shè)置通知標(biāo)題
? ? ? ? ? ? ? ? .setContentText("警告您的電話正在接受監(jiān)聽(tīng)")//設(shè)置通知內(nèi)容
? ? ? ? ? ? ? ? .setAutoCancel(true)//用戶觸摸時(shí),自動(dòng)關(guān)閉
? ? ? ? ? ? ? ? .setContentIntent(pendingIntent)
.setOngoing(true);//設(shè)置處于運(yùn)行狀態(tài)
? ? ? ? //向系統(tǒng)注冊(cè)通知渠道潮峦,注冊(cè)后不能改變重要性以及其他通知行為
? ? ? ? notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//構(gòu)建通知渠道
? ? ? ? NotificationChannel channel =null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
channel =new NotificationChannel(CHANNEL_ID, channelName, importance);
channel.setDescription("錄音");
notificationManager.createNotificationChannel(channel);
}
//將服務(wù)置于啟動(dòng)狀態(tài) NOTIFICATION_ID指的是創(chuàng)建的通知的ID
? ? ? ? startForeground(11,builder.build());
}
@Override
? ? public int onStartCommand(Intent intent,int flags,int startId) {
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
////? ? ? ? ? ? ? ? ? ? ? ? //啟動(dòng)監(jiān)聽(tīng).傳入一個(gè)listener和監(jiān)聽(tīng)的事件,
? ? ? ? tm.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE);
return START_STICKY;
}
@Override
? ? public void onDestroy() {
super.onDestroy();
Log.e("TAG","service onDestroy");
notificationManager.cancel(11);
}
@Override
? ? public IBinder onBind(Intent intent) {
return null;
}
class MyListenerextends PhoneStateListener {
//在電話狀態(tài)改變的時(shí)候調(diào)用
? ? ? ? @Override
? ? ? ? public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//空閑狀態(tài)
//? ? ? ? ? ? ? ? ? ? RxToast.showToast("通話中");
? ? ? ? ? ? ? ? ? ? if (isFirst) {
Toast.makeText(RecorderService.this,"開(kāi)始錄音", Toast.LENGTH_SHORT).show();
isFirst =false;
}else {
Toast.makeText(RecorderService.this,"掛斷電話", Toast.LENGTH_SHORT).show();
}
if (recorder !=null) {
recorder.stop();//停止錄音
? ? ? ? ? ? ? ? ? ? ? ? recorder.release();//釋放資源
? ? ? ? ? ? ? ? ? ? ? ? recorder =null;
}
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.e("TAG","撥入電話,鈴聲響起");
Toast.makeText(RecorderService.this,"撥入電話,鈴聲響起", Toast.LENGTH_SHORT).show();
//? ? ? ? ? ? ? ? ? ? //響鈴狀態(tài)? 需要在響鈴狀態(tài)的時(shí)候初始化錄音服務(wù)
? ? ? ? ? ? ? ? ? ? if (recorder ==null) {
recorder =new MediaRecorder();//初始化錄音對(duì)象
? ? ? ? ? ? ? ? ? ? ? ? recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//設(shè)置錄音的輸入源(麥克)
? ? ? ? ? ? ? ? ? ? ? ? recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//設(shè)置音頻格式(3gp)
? ? ? ? ? ? ? ? ? ? ? ? createRecorderFile();//創(chuàng)建保存錄音的文件夾
? ? ? ? ? ? ? ? ? ? ? ? recorder.setOutputFile("sdcard/recorder" +"/" + getCurrentTime() +".3gp");//設(shè)置錄音保存的文件
? ? ? ? ? ? ? ? ? ? ? ? recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//設(shè)置音頻編碼
? ? ? ? ? ? ? ? ? ? ? ? try {
recorder.prepare();//準(zhǔn)備錄音
? ? ? ? ? ? ? ? ? ? ? ? }catch (IOException e) {
e.printStackTrace();
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(RecorderService.this,"啟動(dòng)錄音", Toast.LENGTH_SHORT).show();
//摘機(jī)狀態(tài)(接聽(tīng))
? ? ? ? ? ? ? ? ? ? if (recorder !=null) {
recorder.start();//接聽(tīng)的時(shí)候開(kāi)始錄音
? ? ? ? ? ? ? ? ? ? ? ? Log.e("TAG","開(kāi)始錄音");
}else {
recorder =new MediaRecorder();//初始化錄音對(duì)象
? ? ? ? ? ? ? ? ? ? ? ? recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//設(shè)置錄音的輸入源(麥克)
? ? ? ? ? ? ? ? ? ? ? ? recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//設(shè)置音頻格式(3gp)
? ? ? ? ? ? ? ? ? ? ? ? recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
createRecorderFile();//創(chuàng)建保存錄音的文件夾
? ? ? ? ? ? ? ? ? ? ? ? recorder.setOutputFile("sdcard/recorder" +"/" + getCurrentTime() +".3gp");//設(shè)置錄音保存的文件
? ? ? ? ? ? ? ? ? ? ? ? try {
recorder.prepare();//準(zhǔn)備錄音
? ? ? ? ? ? ? ? ? ? ? ? }catch (IOException e) {
e.printStackTrace();
}
recorder.start();//接聽(tīng)的時(shí)候開(kāi)始錄音
? ? ? ? ? ? ? ? ? ? ? ? Log.e("TAG","錄音失敗");
}
break;
}
}
//創(chuàng)建保存錄音的目錄
? ? ? ? private void createRecorderFile() {
String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
String filePath = absolutePath +"/recorder";
File file =new File(filePath);
if (!file.exists()) {
file.mkdir();
}
}
//獲取當(dāng)前時(shí)間,以其為名來(lái)保存錄音
? ? ? ? private String getCurrentTime() {
SimpleDateFormat format =new SimpleDateFormat("yyyyMMddHHmmss");
Date date =new Date();
String str = format.format(date);
return str;
}
}
}
//在需要的地方啟動(dòng)服務(wù)
public void btn(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
}else {
startService(intent);
}
}
下章是錄音文件的讀取和播放