我制作了一个电话记录器,可以记录所有在以前的android studio上运行良好的呼入和呼出电话,但是随着我更新android studio,在拨打电话时应用程序崩溃了。我已经更新了所有SDK,Gradle文件,属性,但仍然无法正常工作。以下是PhoneStateReceiver.java文件和错误屏幕截图。欢迎提出建议。
错误日志屏幕截图-https://i.stack.imgur.com/dpeIy.png
public void onReceive(Context context, Intent intent) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
Boolean switchCheckOn = pref.getBoolean("switchOn", true);
if (switchCheckOn) {
try {
System.out.println("Receiver Start");
// boolean callWait=pref.getBoolean("recordStarted",false);
Bundle extras = intent.getExtras();
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.d(TAG, " onReceive: " + state);
Toast.makeText(context, "Call detected(Incoming/Outgoing) " + state, Toast.LENGTH_SHORT).show();
if (extras != null) {
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Log.d(TAG1, " Inside " + state);
/*int j=pref.getInt("numOfCalls",0);
pref.edit().putInt("numOfCalls",++j).apply();
Log.d(TAG, "onReceive: num of calls "+ pref.getInt("numOfCalls",0));*/
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)/*&& pref.getInt("numOfCalls",1)==1*/) {
int j = pref.getInt("numOfCalls", 0);
pref.edit().putInt("numOfCalls", ++j).apply();
Log.d(TAG, "onReceive: num of calls " + pref.getInt("numOfCalls", 0));
Log.d(TAG1, " recordStarted in offhook: " + recordStarted);
Log.d(TAG1, " Inside " + state);
phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d(TAG1, " Phone Number in receiver " + phoneNumber);
if (pref.getInt("numOfCalls", 1) == 1) {
Intent reivToServ = new Intent(context, RecorderService.class);
reivToServ.putExtra("number", phoneNumber);
Log.d("fatal", "onReceive: " + reivToServ.toString());
//name=new CommonMethods().getContactName(phoneNumber,context);
int serialNumber = pref.getInt("serialNumData", 1);
new DatabaseManager(context).addCallDetails(new CallDetails(serialNumber, phoneNumber, new CommonMethods().getTIme(), new CommonMethods().getDate()));
List<CallDetails> list = new DatabaseManager(context).getAllDetails();
for (CallDetails cd : list) {
String log = "Serial Number : " + cd.getSerial() + " | Phone num : " + cd.getNum() + " | Time : " + cd.getTime1() + " | Date : " + cd.getDate1();
Log.d("Database ", log);
}
//recordStarted=true;
pref.edit().putInt("serialNumData", ++serialNumber).apply();
pref.edit().putBoolean("recordStarted", true).apply();
}
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
int k = pref.getInt("numOfCalls", 1);
pref.edit().putInt("numOfCalls", --k).apply();
int l = pref.getInt("numOfCalls", 0);
Log.d(TAG1, " Inside " + state);
recordStarted = pref.getBoolean("recordStarted", false);
Log.d(TAG1, " recordStarted in idle :" + recordStarted);
if (recordStarted && l == 0) {
Log.d(TAG1, " Inside to stop recorder " + state);
context.stopService(new Intent(context, RecorderService.class));
pref.edit().putBoolean("recordStarted", false).apply();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}