Get mobile no or sim serial no in a call - android

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"/>

Related

ACTION_PHONE_STATE_CHANGED not called on network cell changes

I am trying to make my broadcast receiver fire when the phone goes in and out of reception areas. The issue is the receiver never gets called when the cell reception changes. The BroadcastReceiver works fine for getting phone call states (call idle, started ect...), and also for getting the airplane mode switched on and off because the broadcast receiver handles both.
I added the permissions and intent filter to the receiver in the manifest and they are working fine.
Here is what I have for my BroadcastReceiver and PhoneStateListener.
public class PhoneStateReceiver extends BroadcastReceiver {
private PhoneStateListener mListener = new ServiceStateListener();
private TelephonyManager mTelephonyManager;
private Context mContext;
/**
* TODO add some sort of call back interface to allow for different uses of this phone state receiver
*/
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
mContext = context;
if (action.intern().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
if (!isAirplaneModeOn) {
SmsRetryManager.getInstance().retryAllSms(context);
}
} else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
mTelephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
Toast.makeText(mContext, "Receiver registered!", Toast.LENGTH_LONG).show();
mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_SERVICE_STATE);
mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
}
public void onDestroy() {
mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_NONE);
}
private class ServiceStateListener extends PhoneStateListener {
#Override
public void onServiceStateChanged (ServiceState serviceState) {
super.onServiceStateChanged(serviceState);
boolean connected = (serviceState.getState() == ServiceState.STATE_IN_SERVICE);
if (connected) {
Toast.makeText(mContext, "Connection Gained!", Toast.LENGTH_LONG).show();
//todo retry sms here
SmsRetryManager.getInstance().retryAllSms(mContext);
} else {
Toast.makeText(mContext, "Connection Lost!", Toast.LENGTH_LONG).show();
}
}
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
Toast.makeText(mContext, "Signal changed - cdma : " + signalStrength.getCdmaDbm() + " gsm : " + signalStrength.getGsmSignalStrength(), Toast.LENGTH_LONG).show();
}
}
Any insight would be awesome. I have been banging my head on this one for a while.
Thanks!
I assume you are listening the broadcast android.intent.action.SERVICE_STATE
If so, try using:
if (TelephonyManager.getnetworkOperator.length()==0) connected=false;
on your OnReceive method, to know if the phone is not connected. It works fine for me.
If this solution doesn't work, please show how you register the receiver: is it statically registered at manifest.xml? or dynamically with PackageManager.setComponentEnabledSetting? Note: With static registration you'll find the receiver is not triggered after reinstalling the app, needing to add to the receiver tag
<intent-filter>
<action android:name= "android.intent.action.MY_PACKAGE_REPLACED"/></intent-filter>
You can also look at the values that return the ServiceState.
See here http://developer.android.com/reference/android/telephony/ServiceState.html
And check which of these values is returned:
int STATE_EMERGENCY_ONLY = The phone is registered and locked. Only
emergency numbers are allowed.
int STATE_IN_SERVICE = Normal operation condition, the phone is registered with an operator either in home network or in roaming.
int STATE_OUT_OF_SERVICE = Phone is not registered with any operator,
the phone can be currently searching a new operator to register to, or
not searching to registration at all, or registration is denied, or
radio signal is not available.
int STATE_POWER_OFF = Radio of telephony is explicitly powered off.

Android Check if call forwarding is activated

I am building a call forwarding application and have used the **21*xxxxxx# ussd code to activate call fowarding using ACTION_CALL Intent. But I have not found a solution to check whether Call forwarding is active or not.
Is there any solution to check from the android system if call forwarding is active or not?
TelephonyManager manager = (TelephonyManager)
this.getSystemService(TELEPHONY_SERVICE);
manager.listen(new MyPhoneStateListener(),
PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR );
class MyPhoneStateListener extends PhoneStateListener{
#Override
public void onCallForwardingIndicatorChanged(boolean cfi) {
Log.i(TAG,"onCallForwardingIndicatorChanged CFI ="+cfi);
preferences.edit().putBoolean("CALL_FORWARD_ACTIVE", cfi).commit();
super.onCallForwardingIndicatorChanged(cfi);
}
}
if cfi returns true that means set call forward success
you can make Brodcast class register it and you can track out going call like
public class CallBroadcastReceiver extends BroadcastReceiver
{
public static String numberToCall;
public void onReceive(Context context, Intent intent) {
Log.d("CallRecorder", "CallBroadcastReceiver::onReceive got Intent: " + intent.toString());
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
numberToCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d("CallRecorder", "CallBroadcastReceiver intent has EXTRA_PHONE_NUMBER: " + numberToCall);
}
}
}
PRECAUTION:
The following answer is suggestion. I haven't tried it personally So you better try it to check the result.
From your gsm number you can check your call forwarding options by dialing *#21#. So you can try dialing this number from application and read the ussd response.
part 1: to dial the number
Intent intentCallForward = new Intent(Intent.ACTION_CALL);
Uri uri = Uri.fromParts("tel", "*#21#", "#");
intentCallForward.setData(uri);
startActivity(intentCallForward);
part 2: to read the ussd response
There is no API to do this. But here in this SO answer it suggested some methods that you can try.
Best of luck
yes you can use *#21# to check for CF in the same way as you used **21*xxxxxx# to activate CF.
BTW, these are MMI code, not USSD code.

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

How to get device id in a onRecive function

im trying to get the device id inside a onRecive (which is inside broadcastreceiver) but the only way i found is to use this code:
TelephonyManager tManager = (TelephonyManager)myActivity.getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
device_id = tManager.getDeviceId();
but here i am not in an Activity. so how can i get the device id?
Thanks!
If you are on onReceive then you have a Context.
If you have a context then you can call getSystemService and do the same thing as here
use this:
public class PhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tManager = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
device_id = tManager.getDeviceId();
}
}

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