Android8.0電話監聽服務

1創建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;//錄音的一個實例

? ? //通過binder實現調用者client與Service之間的通信

? ? private NotificationCompat.Builderbuilder;

private NotificationManagernotificationManager;

private boolean isFirst =true;

public RecorderService() {

}

@Override

? ? public void onCreate() {

super.onCreate();

//如果API在26以上即版本為O則調用startForefround()方法啟動服務

//? ? ? ? if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

? ? ? ? setForegroundService();

//? ? ? ? }else {

//

//? ? ? ? }

? ? }

/**

? ? * 通過通知啟動服務

? ? */

? ? public void setForegroundService() {

//設定的通知渠道名稱

? ? ? ? String channelName = getString(R.string.channel_name);

final String CHANNEL_ID ="com.appname.notification.channel";

//設置通知的重要程度

? ? ? ? int importance = NotificationManager.IMPORTANCE_LOW;

Intent mIntent =new Intent(this, MainActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this,0, mIntent,0);

//在創建的通知渠道上發送通知

? ? ? ? builder =new NotificationCompat.Builder(this, CHANNEL_ID);

builder.setSmallIcon(R.mipmap.ic_launcher)//設置通知圖標

? ? ? ? ? ? ? ? .setContentTitle("電話監聽")//設置通知標題

? ? ? ? ? ? ? ? .setContentText("警告您的電話正在接受監聽")//設置通知內容

? ? ? ? ? ? ? ? .setAutoCancel(true)//用戶觸摸時,自動關閉

? ? ? ? ? ? ? ? .setContentIntent(pendingIntent)

.setOngoing(true);//設置處于運行狀態

? ? ? ? //向系統注冊通知渠道,注冊后不能改變重要性以及其他通知行為

? ? ? ? notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//構建通知渠道

? ? ? ? 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);

}

//將服務置于啟動狀態 NOTIFICATION_ID指的是創建的通知的ID

? ? ? ? startForeground(11,builder.build());

}

@Override

? ? public int onStartCommand(Intent intent,int flags,int startId) {

TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

////? ? ? ? ? ? ? ? ? ? ? ? //啟動監聽.傳入一個listener和監聽的事件,

? ? ? ? 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 {

//在電話狀態改變的時候調用

? ? ? ? @Override

? ? ? ? public void onCallStateChanged(int state, String incomingNumber) {

super.onCallStateChanged(state, incomingNumber);

switch (state) {

case TelephonyManager.CALL_STATE_IDLE:

//空閑狀態

//? ? ? ? ? ? ? ? ? ? RxToast.showToast("通話中");

? ? ? ? ? ? ? ? ? ? if (isFirst) {

Toast.makeText(RecorderService.this,"開始錄音", 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();

//? ? ? ? ? ? ? ? ? ? //響鈴狀態? 需要在響鈴狀態的時候初始化錄音服務

? ? ? ? ? ? ? ? ? ? if (recorder ==null) {

recorder =new MediaRecorder();//初始化錄音對象

? ? ? ? ? ? ? ? ? ? ? ? recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//設置錄音的輸入源(麥克)

? ? ? ? ? ? ? ? ? ? ? ? recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//設置音頻格式(3gp)

? ? ? ? ? ? ? ? ? ? ? ? createRecorderFile();//創建保存錄音的文件夾

? ? ? ? ? ? ? ? ? ? ? ? recorder.setOutputFile("sdcard/recorder" +"/" + getCurrentTime() +".3gp");//設置錄音保存的文件

? ? ? ? ? ? ? ? ? ? ? ? recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//設置音頻編碼

? ? ? ? ? ? ? ? ? ? ? ? try {

recorder.prepare();//準備錄音

? ? ? ? ? ? ? ? ? ? ? ? }catch (IOException e) {

e.printStackTrace();

}

}

break;

case TelephonyManager.CALL_STATE_OFFHOOK:

Toast.makeText(RecorderService.this,"啟動錄音", Toast.LENGTH_SHORT).show();

//摘機狀態(接聽)

? ? ? ? ? ? ? ? ? ? if (recorder !=null) {

recorder.start();//接聽的時候開始錄音

? ? ? ? ? ? ? ? ? ? ? ? Log.e("TAG","開始錄音");

}else {

recorder =new MediaRecorder();//初始化錄音對象

? ? ? ? ? ? ? ? ? ? ? ? recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//設置錄音的輸入源(麥克)

? ? ? ? ? ? ? ? ? ? ? ? recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//設置音頻格式(3gp)

? ? ? ? ? ? ? ? ? ? ? ? recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

createRecorderFile();//創建保存錄音的文件夾

? ? ? ? ? ? ? ? ? ? ? ? recorder.setOutputFile("sdcard/recorder" +"/" + getCurrentTime() +".3gp");//設置錄音保存的文件

? ? ? ? ? ? ? ? ? ? ? ? try {

recorder.prepare();//準備錄音

? ? ? ? ? ? ? ? ? ? ? ? }catch (IOException e) {

e.printStackTrace();

}

recorder.start();//接聽的時候開始錄音

? ? ? ? ? ? ? ? ? ? ? ? Log.e("TAG","錄音失敗");

}

break;

}

}

//創建保存錄音的目錄

? ? ? ? private void createRecorderFile() {

String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();

String filePath = absolutePath +"/recorder";

File file =new File(filePath);

if (!file.exists()) {

file.mkdir();

}

}

//獲取當前時間,以其為名來保存錄音

? ? ? ? private String getCurrentTime() {

SimpleDateFormat format =new SimpleDateFormat("yyyyMMddHHmmss");

Date date =new Date();

String str = format.format(date);

return str;

}

}

}


//在需要的地方啟動服務

public void btn(View view) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

startForegroundService(intent);

}else {

startService(intent);

}

}

下章是錄音文件的讀取和播放

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容