So far, it has not been possible to solve the problem entirely, but the required result has been partially obtained.
My task is to timely send information about incoming calls to the server. Initially, when the second line appeared, the problem was that at the end of the call, the server received an info only about the last call, and the first one hung active. This problem was solved by storing numbers in a local database and viewing this database as IDLE. The problem with hanging active calls was resolved, but another one appeared - regardless of the actions that took place in the “Hour Hr”, both IDLE calls received one ending date and none of them could be missed. Google’s several-hour picking has brought CallLogs.Call to the class and now, when the phone goes into IDLE mode, numbers are taken from the local database (ActiveCallsDB in the code) and CallLogs.Call gets the duration of the last conversation with this number. My specific problem has been solved, although it’s not elegant, I will be glad if someone comes in handy. But the question remains open.
if (state.equals("RINGING")) { ringing = true; number = incomingNumber; intentForService.putExtra("number", number); cont.startService(intentForService); SQLiteDatabase db = (new ActiveCallsDB(cont)).getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("phone", number); cv.put("status", "0"); db.insert(ActiveCallsDB.TABLE_NAME, null, cv); db.close(); } else if (ringing && state.equals("OFFHOOK")) { intentForService.putExtra("number", number); intentForService.putExtra("state", 2); cont.startService(intentForService); SQLiteDatabase db = (new ActiveCallsDB(cont)).getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("phone", number); cv.put("status", "2"); db.update(ActiveCallsDB.TABLE_NAME, cv, "phone = ?", new String[] { number }); db.close(); } else if (ringing && state.equals("IDLE")) { try { synchronized (this) { // Ждём, пока ин-фа о звонках запишется в базу Android-а this.wait(1000); } SQLiteDatabase db = (new ActiveCallsDB(cont)).getReadableDatabase(); Cursor cur = db.rawQuery("SELECT * FROM " + ActiveCallsDB.TABLE_NAME, null); if (cur.moveToFirst()) { do { String phoneNumber = cur.getString(cur.getColumnIndex("phone")); String[] projection = new String[] { Calls.NUMBER, Calls.DURATION }; Cursor curForCall = cont.getContentResolver().query(Calls.CONTENT_URI, projection, "number = ?", new String[] { phoneNumber }, Calls.DATE + " desc"); curForCall.moveToFirst(); String duration = curForCall.getString(1); curForCall.close(); intentForService.putExtra("number", phoneNumber); intentForService.putExtra("state", 1); intentForService.putExtra("duration", duration); cont.startService(intentForService); db.delete(ActiveCallsDB.TABLE_NAME, "id = " + cur.getInt(cur.getColumnIndex("id")), null); } while (cur.moveToNext()); } ringing = false; } catch (Throwable e) { e.printStackTrace(); } }