How to find out which SIM is receiving an incoming call in android? - android

I am listening to a broadcast receiver. In a dual SIM, how do I know which SIM is receiving an incoming call. The intent bundle only contains the state and the incoming phone number.
public void onReceive(Context context, Intent intent) {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}

Related

Call or text back to a private number

I'm using BroadcastReceiver to determine that calls or messages are from private numbers.
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Bundle onReceiveBundle = intent.getExtras();
String phoneNumber = onReceiveBundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
The phoneNumber returns negative numbers which cannot be called or replied back. Without the need to decode it, is it possible to reply or call back to a private number? If not, how can I decode it?

Error in broadcast receiver for android, it listens to all SMSs that my phone receives

I'm doing an encryption SMS app, in which, user can encrypt the text and send SMS through my apps.
I used the following broadcast receiver.
The problem is that its listen to all the SMS that come to my phone.
How to make it so that it will only listen to the SMS sent from my apps? Other sms's should open as normal, using default SMS application
public class SmsBroadCastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
// Specify the bundle to get object based on SMS protocol "pdus"
Object[] object = (Object[]) bundle.get("pdus");
SmsMessage sms[] = new SmsMessage[object.length];
Intent in=new Intent(context,DisplaySMSActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
String msgContent = "";
String originNum = "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < object.length; i++) {
sms[i] = SmsMessage.createFromPdu((byte[]) object[i]);
// get the received SMS content
msgContent = sms[i].getDisplayMessageBody();
//get the sender phone number
originNum = sms[i].getDisplayOriginatingAddress();
//aggregate the messages together when long message are fragmented
sb.append(msgContent);
//abort broadcast to cellphone inbox
abortBroadcast();
}
//fill the sender's phone number into Intent
in.putExtra("originNum", originNum);
//fill the entire message body into Intent
in.putExtra("msgContent", new String(sb));
//start the DisplaySMSActivity.java
context.startActivity(in);
}
Maybe you are not correctly registering your receiver.
When declaring a Receiver in the manifest (or programmatically), you can also specify an intent filter. You can specify the 'action' you want to receive in your receiver.
For example: "com.your_app_package.sms_encrypted_msg".
Either way, remember to check the action in the onReceive method:
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals('com.your_app_package.sms_encrypted_msg')){
...
}
}

Get mobile no or sim serial no in a call

when we receive a call we can get incoming mobile no by below mentioned code,but If it is a dual sim mobile How do I get my ringing number or sim serial number ?Please provide me any solution for this matter.
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
strPhoneNumber =intent.getExtras().getString("incoming_number");
}
}
TelephonyManager telMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String simLineNumber = telMgr.getLine1Number();
obviously, you'll need to ask for:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Getting the outbound phonenumber in Android

Bellow a simplified piece of my code is shown. The important part is that I now have phone number when the call state is CALL_STATE_RINGING and the call is inbound. Now I also want the phone number when the call is outbound (so the phone number on the receiving end). What am I missing here?
#Override
public void onCallStateChanged(int state, String number) {
Log.d("BackgroundService", "State: "+ state +" Number: " + number);
}
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_STATE);
Register a broadcast receiver using ACTION_NEW_OUTGOING_CALL. In the onReceive callback function you will know the number of outgoing call
public void onReceive(Context context, Intent intent) {
String phone = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
And offcourse use the permission android.permission.PROCESS_OUTGOING_CALLS

Android SIM change

Is it possible to detect SIM number using TelephonyManager in android at boot startup ,using Service at bootup...
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String ss=tm.getSimSerialNumber();
You need to register a broadcast receiver for the boot completion action i.e android.intent.action.BOOT_COMPLETED
in onReceive of this receiver you can start your service get SIM number with below code lines
TelephonyManager telephoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = telephoneMgr.getLine1Number();
Also need to have permission for reading phone number as READ_PHONE_STATE in manifest file.
you can start service from broadcast receiver as -
public class BootListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context,Myservice.class);
context.startService(intent);
}
}

Categories

Resources