0

I implemented interface BroadcastReceiver for hook incoming call and sms. I want get simId to which the incoming call or sms comes. How to do it?

My code for receiver calls and sms:

public class SmsAndCallReceiver extends BroadcastReceiver {

    public void _onReceive(Context context, Intent intent) {
        // here I define incoming call or sms
        // here I get simId !!!
    }
}

I also registered class in AndroidManifest.xml:

<receiver android:name=".receivers.SmsAndCallReceiver" android:exported="true">
    <intent-filter>
       <action android:name="android.intent.action.PHONE_STATE"/>
    </intent-filter>
</receiver>

And also I add permissions:

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>

I saw this option, but it doesn't work on my device:

public class MyCallReceiver extends BroadcastReceiver {
    
    @Override
    public void onReceive(Context context, Intent intent) {
        String callingSIM = "";
        callingSIM = intent.getExtras().getInt("simId", -1);
        if(callingSIM == "0"){
            //SIM1
        }
        else if(callingSIM =="1"){
            //SIM2
        }
    }

}

My device: Samsung SM-J260F

Version android: 8.1.0 (API 27)

Root device: no

Range
  • 416
  • 7
  • 20
  • The key for that extra, if it's there, can vary quite a bit, from the examples I've seen. If you just need it for that specific device, you could simply dump all the extras and see what's there: https://stackoverflow.com/a/15074150. – Mike M. Mar 20 '22 at 15:03
  • @MikeM. I have tried this. I only see this: state and incoming_number – Range Mar 20 '22 at 15:04
  • Yeah, unless it's included in one of those extras somehow, it looks like it might not be available on that broadcast for that device. – Mike M. Mar 20 '22 at 15:05
  • @MikeM. Hmm...Don't buy a new device? :) – Range Mar 20 '22 at 15:21
  • Do you absolutely have to have that information in the `BroadcastReceiver`? Or can you look it up later, like when you're launching an `Activity`, for example? That information should eventually be available in the Call and SMS Providers, allowing you to query for it – i.e., through `getContentResolver().query(...)` – but it might not be saved there yet when those broadcasts happen. Or, at least, there's likely no guarantee that it will be saved by then. – Mike M. Mar 20 '22 at 15:31
  • @MikeM. Yes, now I'm requesting this information from CallLog.Calls and Telephony.Sms.CONTENT_URI. But here problems can arise with an asynchronous call: another number is ringing, while the first call has not had time to access the log yet. Therefore, the first call will receive information about the second call. I make a log access delay of 5 seconds. – Range Mar 20 '22 at 15:37
  • Hmm, yeah, I see what you were aiming for, then. The only thing I can think of atm is to delay your processing until later. If you need this real-time, I can't think of any solid suggestion, offhand. – Mike M. Mar 20 '22 at 15:44
  • Now that I've thought a bit, I suppose you could implement some sort of `Service` and `ContentObserver` setup. For example, when a call comes in, start the `Service`, passing it the incoming number on its `Intent`, and have it register a `ContentObserver` on the Call Provider to keep track of any other calls that might come in concurrently. When the last number hangs up (there might be only the initial one), unregister the `ContentObserver`, and have the `Service` stop itself. Not exactly as simple as grabbing an extra, but it should be workable, I'd imagine. – Mike M. Mar 20 '22 at 16:18
  • 1
    Also, depending on the overall project and requirements, you could simply have a foreground `Service` running with those `ContentObserver`s registered all the time. You might not even need the Receivers, in that case. I only mention this option because it sounds like this might be a personal project, or a specialized one-off, or something. I wouldn't ever recommend running a constant `Service` in an app for general distribution. Just FYI. – Mike M. Mar 20 '22 at 16:28
  • @MikeM. this is a personal project :) – Range Mar 20 '22 at 18:04
  • 1
    @MikeM. did once a minute getting data from the content resolver Calls and smd. Works great! – Range Mar 22 '22 at 08:18

0 Answers0