在拨出电话

时间:2017-04-20 16:51:19

标签: android broadcastreceiver android-service android-4.4-kitkat mediarecorder

我正在尝试使用广播接收器和后台服务制作自动呼叫记录器。

package com.mak.socialmediacallrecorder;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Toast;

import java.io.IOException;

public class BgService extends Service {

public MediaRecorder mRecorder;
private static String mFileName = null;

public BgService() {

}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if(mRecorder == null) {

        mRecorder = new MediaRecorder();
        mRecorder.reset();
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/aasimkhanmak.3gp";

        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        // mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        //mRecorder.getMaxAmplitude();

        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        // mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mRecorder.setOutputFile(mFileName);


        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Toast.makeText(this, "Pre " + e, Toast.LENGTH_SHORT).show();
        }

        try {
            mRecorder.start();
            Toast.makeText(this, "Start Rec", Toast.LENGTH_SHORT).show();

        } catch (RuntimeException e) {
            Toast.makeText(this, "Start " + e, Toast.LENGTH_SHORT).show();
            mRecorder.release();
            mRecorder = null;

        }


    }

    return super.onStartCommand(intent, flags, startId);
}


@Override
public void onDestroy() {
    super.onDestroy();

    if(mRecorder != null) {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
        Toast.makeText(this, "Call End...Stop Rec", Toast.LENGTH_SHORT).show();
    }

}
}

此代码适用于我的三星galaxy note5的传入和传出呼叫,但在其他具有kitkat 4.4.4的移动设备上,此代码无法正常工作,它仅适用于来电但是当我拨打电话时MediaRecorder启动失败。

如果有人有任何解决方案,请与我分享。

0 个答案:

没有答案