一、不用service实现后台录音功能

1.在onPause()方法内实现:

if (mRecorder.state() == Recorder.RECORDING_STATE) {

if (mMode == true) {
mRecorder.stop();
return;
}

Intent notificationIntent = new Intent("android.provider.MediaStore.RECORD_SOUND");

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

long when = System.currentTimeMillis(); // notification time

Context context = getApplicationContext();

Notification notification = new Notification(R.drawable.ic_launcher_soundrecorder,null, when);

Resources res = getResources();

String message1 = res.getString(R.string.app_name);

String message2 = res.getString(R.string.recording_in_progress);
notification.setLatestEventInfo(context, message1, message2, contentIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.notify(NOTIFICATION_ID, notification);
mInNotification = true;
}

2.onResume()方法内去掉通知栏内的通知

if (mInNotification == true) {

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

mNotificationManager.cancel(NOTIFICATION_ID);

mInNotification = false;
}

3.在androidmanifext.xml中设置activity mode为singleTop。



二、录音文件使用当前时间

public String getCurrentTime(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
String nowTime=sdf.format(new Date());
return nowTime;
}
public File createTempFile_test(String prefix, String suffix, File directory)
throws IOException {
// Force a prefix null check first
if (prefix.length() < 3) {
throw new IllegalArgumentException("prefix must be at least 3 characters");
}
if (suffix == null) {
suffix = ".tmp";
}
File tmpDirFile = directory;
if (tmpDirFile == null) {
String tmpDir = System.getProperty("java.io.tmpdir", ".");
tmpDirFile = new File(tmpDir);
}
File result;
result = new File(tmpDirFile, prefix + getCurrentTime() + suffix);
if(result.exists()){
result.delete();
}
if(!result.exists()){
result.createNewFile();
}
return result;
}


三。解决先开启一个录音播放器,按home键进去后台播放,此时通过短信附件开启录音机,这时应该关闭后台的那个录音机

解决办法使用广播进行关闭,代码如下:


//处理广播

public class SoundRecorder extends Activity
implements Button.OnClickListener, Recorder.OnStateChangedListener, OnSeekBarChangeListener {
private BroadcastReceiver mReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(mRecorder!=null){
mRecorder.stop();
saveSample();
mRecorder.clear();
}
if (mInNotification == true) {


String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);


mNotificationManager.cancel(NOTIFICATION_ID);


mInNotification = false;
}


}
};

//onCreate()中先发送广播再注册广播,第一次就接受不到广播,等第三方应用调用时起作用

Intent intent=new Intent();
intent.setAction(ACTION_STOP);
sendBroadcast(intent);


IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_STOP);
this.registerReceiver(mReceiver, filter);

更多相关文章

  1. Android(安卓)N新特性
  2. android 显示通知栏
  3. Android(安卓)NotificationListenerService监听获取通知栏通知和
  4. android实现状态栏添加图标的函数实例
  5. Android(安卓)实现Activity后台运行
  6. android强制完全退出核心代码
  7. Android安全检查之Activity劫持检测
  8. 利用Google GCM发送push通知到Android客户端
  9. android实现通知栏透明

随机推荐

  1. Android更新ADT到R17以后,不能用第三方ja
  2. Attribute is missing the Android(安卓)
  3. Android中使EditText失去焦点,edittext禁
  4. CardView 设置水波纹效果
  5. Android(安卓)常用控件(三)学习笔记
  6. Android(安卓)Q : 安卓源码、水滴屏适配
  7. Android(安卓)Unable to resolve target
  8. android layout属性 .
  9. Cordova 3.x 基础(13) -- 为Android(安卓)A
  10. Android画图学习笔记一 类的简介