Я работаю над приложением с управлением звонками.Чтобы определить, когда на вызов получен ответ, я попытался создать класс, унаследовавший BroadcastReceiver
и прослушивающий события EXTRA_INCOMING_NUMBER
, ACTION_NEW_OUTGOING_CALL
и TelephonyManager.EXTRA_STATE
.
Он работает, как и ожидалось, в версии Android ниже 7.0но он не перехватывает ни одного события в версии 8.1 и выше.
public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
this.context = context;
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("On recieve satate");
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER).toString();
}
if (number != null) {
number = number.trim();
number = number.replace("+91", "");
}
else {
Toast.makeText(context,"UNABLE ", Toast.LENGTH_LONG).show();
}
geturl= SplashScreen.getURL();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
operator_name = tm.getNetworkOperatorName();
if (stateStr != null) {
if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
// isExist = clientDataBase.isNumberExists2(number);
//opendialogg("","","","","");
System.out.println(" inside state");
if (isExist == true) {
DatabaseRegistration clientDataBase1 = new DatabaseRegistration(context);
RegistrationSetterGetter setterGetter = new RegistrationSetterGetter();
setterGetter = clientDataBase1.getDataOfSingleClient2(number);
USERname = setterGetter.getName();
INDUStype = setterGetter.getIndusrtyName();
ORGname = setterGetter.getCompanyName();
Log.d("RESPONSE",USERname+" "+INDUStype+" "+ORGname);
opendialogg(operator_name, number, USERname, INDUStype, ORGname);
} else {
progressBar = new ProgressBar(context);
getNumberDeatilsOperation(number, operator_name);
}
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
sendToHost();
}
}, 3000);
}
else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
getInitial();
context.startService(new Intent(context, MyRecordingService.class).putExtra("initcall", datess));
}
}
else if (stateStr == null) {
System.out.println("Null State"+stateStr);
DatabaseRegistration dbAutoSave = new DatabaseRegistration(context);
isExist = dbAutoSave.isNumberExists2(number);
if (isExist == true) {
RegistrationSetterGetter setterGetter = new RegistrationSetterGetter();
setterGetter = dbAutoSave.getDataOfSingleClient2(number);
USERname = setterGetter.getName();
INDUStype = setterGetter.getIndusrtyName();
ORGname = setterGetter.getCompanyName();
System.out.print("VALUE OF EXISTING DATA PHASE");
Log.d("RESPONSE",USERname+" "+INDUStype+" "+ORGname);
opendialogg(operator_name, number, USERname, INDUStype, ORGname);
} else {
Log.d("RESPONSE","");
progressBar = new ProgressBar(context);
getNumberDeatilsOperation(number, operator_name);
}
// opendialogg("","","","","");
}
else {
System.out.println("NOT DETECT STATE");
}
}
внешний метод onReceive вызов метода sendToHost ();выглядит как
public void sendToHost() {
getSendData();
}
public void getSendData() {
SharedPreferences sharedpreferences = context.getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
id_user = sharedpreferences.getString("userKey", "");
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
return;
}
Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC LIMIT 1");
if (cursor != null) {
cursor.moveToLast();
int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
int date = cursor.getColumnIndex(CallLog.Calls.DATE);
int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);
int id = cursor.getColumnIndex(CallLog.Calls._ID);
phone = cursor.getString(number);
phone = phone.trim();
phone = phone.replace("+91", "");
String callType = cursor.getString(type);
callDate = cursor.getLong(date);
ids = cursor.getString(id);
datess = new SimpleDateFormat("yyy-MM-dd H:mm:ss", Locale.getDefault()).format(callDate);
callDur = cursor.getString(duration);
dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
cursor.close();
}
}
}
Манифест выглядит так:
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission
android:name="android.permission.MODIFY_PHONE_STATE"
tools:ignore="ProtectedPermissions"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-feature android:name="android.hardware.location.gps"/>
<receiver android:name=".Unused.CallReceiver">
<intent-filter>
<action android:name="finishMeeting"/>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>