问题描述
我的应用是自动呼叫记录器,在版本9之前可以正常使用,但在上面崩溃 有人接听电话时出现应用程序崩溃,并给出了无法启动接收器的错误,这两个类都包含错误图像。接收者选择来电时,应用程序崩溃,功能调用getAbsolutePath()上出现错误
public abstract class Receiver_store extends broadcastReceiver {
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savednumber;
static MediaRecorder recorder= new MediaRecorder();
static AudioManager audioManager;
static File audiofile;
Context context;
public static boolean record = false;
public Receiver_store() {
super();
}
@Override
public void onReceive(Context context,Intent intent) {
this.context=context;
audioManager = (AudioManager)context.getSystemService(Context.AUdio_SERVICE);
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savednumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
} else {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
state = TelephonyManager.CALL_STATE_IDLE;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
}
onCallStateChanged(context,state,number);
}
}
protected void onIncomingCallStarted(Context ctx,String number,Date start) {
}
protected void onOutgoingCallStarted(Context ctx,Date start) {
}
protected void onIncomingCallEnded(Context ctx,Date start,Date end) {
}
protected void onOutgoingCallEnded(Context ctx,Date end) {
}
protected void onMissedCall(Context ctx,Date start) {
}
protected void onIncomingCallAnswered(Context context,String savednumber,Date callStartTime) {
}
public void onCallStateChanged(Context context,int state,String number) {
if (lastState == state) {
//No change
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savednumber = number;
onIncomingCallStarted(context,number,callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context,savednumber,callStartTime);
} else {
isIncoming = true;
callStartTime = new Date();
onIncomingCallAnswered(context,callStartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//call ended
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
// a miss call
onMissedCall(context,callStartTime);
} else if (isIncoming) {
onIncomingCallEnded(context,callStartTime,new Date());
isIncoming = false;
} else {
onOutgoingCallEnded(context,new Date());
}
break;
}
lastState = state;
}
public void startRecord(String name){
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(context);
int source=Integer.parseInt(SP.getString("RECORDER","2"));
File sampleDir;
String dir= GetContact.getFolderPath(context);
if(dir.isEmpty()){
String Appname = context.getString(R.string.app_name);
sampleDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"/" + Appname);
}else {
sampleDir = new File(dir);
}
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
String file_name = name;
try {
audiofile = File.createTempFile(file_name,".3gpp",sampleDir);
} catch (IOException e) {
e.printstacktrace();
}
switch (source){
case 0:
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
break;
case 1:
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
audioManager =(AudioManager) context.getSystemService(Context.AUdio_SERVICE);
audioManager.setStreamVolume(3,audioManager.getStreamMaxVolume(3),0);
audioManager.setSpeakerphoneOn(true);
break;
case 2:
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
break;
case 3:
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);
break;
case 4:
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
break;
default:
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
break;
}
recorder.setAudioSamplingRate(8000);
recorder.setAudioEncodingBitRate(12200);
recorder.setoutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setoutputFile(audiofile.getAbsolutePath());
try {
recorder.prepare();
recorder.start();
record = true;
} catch (IllegalStateException e) {
e.printstacktrace();
} catch (IOException e) {
e.printstacktrace();
}
}
public void stopRecording() {
if (record){
recorder.stop();
}
if(audioManager!=null){
audioManager.setSpeakerphoneOn(false);
}
}
并且Receiver扩展类是
public class Receiver_Extended extends Receiver_store {
String formated_number;
@Override
protected void onIncomingCallStarted(Context ctx,Date start) {
//incoming call ringing
}
@Override
protected void onOutgoingCallStarted(Context ctx,Date start) {
//out going call started
formated_number= Utils_String.prepareContacts(ctx,number);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(ctx);
boolean b=SP.getBoolean("STATE",true);
if (b&& GetContact.checkContactToRecord(ctx,number)){
startRecord(formated_number+"__"+ GetContact.getCurrentTimeStamp()+"__"+"OUT__2");
addtoDatabase(ctx,formated_number);
GetContact.sendnotification(ctx);
}
}
@Override
protected void onIncomingCallAnswered(Context ctx,Date start) {
//incoming call answered
formated_number= Utils_String.prepareContacts(ctx,true);
if(b&& GetContact.checkContactToRecord(ctx,number)){
startRecord(formated_number+"__"+ GetContact.getCurrentTimeStamp()+"__"+"IN__2");
addtoDatabase(ctx,formated_number);
GetContact.sendnotification(ctx);
}
}
@Override
protected void onIncomingCallEnded(Context ctx,Date end) {
//incoming call ended
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(ctx);
boolean b=SP.getBoolean("STATE",true);
if(b){
stopRecording();
}
notificationmanager notificationmanager=(notificationmanager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationmanager.cancelAll();
GetContact.sendnotificationOnEnd(ctx);
}
@Override
protected void onOutgoingCallEnded(Context ctx,Date end) {
//outgoing call ended
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(ctx);
boolean b=SP.getBoolean("STATE",true);
if(b){
stopRecording();
}
notificationmanager notificationmanager=(notificationmanager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationmanager.cancelAll();
GetContact.sendnotificationOnEnd(ctx);
}
@Override
protected void onMissedCall(Context ctx,Date start) {
//miss call
}
public void addtoDatabase(Context ctx,String number){
DatabaseHelper db=new DatabaseHelper(ctx);
if(db.isContact(number).getNumber()!=null){
}else{
Contacts_data contacts=new Contacts_data();
contacts.setFav(0);
contacts.setState(0);
contacts.setNumber(number);
db.addContact(contacts);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)