通过广播,实现去电监听,通过服务实现录音。
开发工具:Android Studio 3.0.1
看代码:
broadcastReceiver.java
package com.example.test_6application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class broadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
String phoneNum = intent.getStringExtra( Intent.EXTRA_PHONE_NUMBER );
Toast.makeText( context, "监听到拨打电话:" +phoneNum , Toast.LENGTH_SHORT ).show();
//其他操作,如启动另一个Activiy:
//Intent it = new Intent( context, ***Activity.class );
//context.startActivity(it);
Intent it = new Intent( context,recordService.class );
context.startService(it); //启动录音服务
}
}
}
//先创建一个广播
再创建一个服务(
recordService.java)
//使用MediaRecorder类实现录音
源码:
package com.example.test_6application;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class recordService extends Service {
private File recordFile;
// 录音类
private MediaRecorder mediaRecorder;
public recordService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);
// recordFile = new File("/sdcard", "kk.amr");
recordFile = new File("/sdcard/"+"_"+str+".3gp");
mediaRecorder = new MediaRecorder();
// 判断,若当前文件已存在,则删除
if (recordFile.exists()) {
recordFile.delete();
}
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置音频输入源 ,麦克风
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); //设置音频输出格式 three_gpp
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);//设置音频编码方式 amr_nb
mediaRecorder.setOutputFile(recordFile.getAbsolutePath());
try {
// 准备好开始录音
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
if (recordFile != null) {
mediaRecorder.stop();
mediaRecorder.release();
}
super.onDestroy();
}
}
创建方法:
MainActivity.java
android 6.0以上要获取手机打电话、录音、存储权限,及以下代码的3个 if 条件语句
注意要引入 v4-27.1.1 包
package com.example.test_6application;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.PROCESS_OUTGOING_CALLS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.PROCESS_OUTGOING_CALLS}, 123);
return;
}
if (ActivityCompat.checkSelfPermission( MainActivity.this,
Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions( MainActivity.this,
new String[] { Manifest.permission.RECORD_AUDIO }, 12345);
return;
}
if (ActivityCompat.checkSelfPermission( MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions( MainActivity.this,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 123456);
return;
}
Button bt2=(Button) findViewById(R.id.button2);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent( MainActivity.this,recordService.class );
stopService(it);
}
});
}
}
activitymain.xml
manifests.xml
添加权限:去电权限、写入sd卡权限等
添加广播监听权限:
本人使用android 6.0手机,能够正确运行录音
这是我的github链接:
https://github.com/jiang-congcong/android-------
里面有全部源码。