How to know whether I am in a call on Android?
You need broadcast receiver ...
In manifest declare broadcast receiver ...
<receiver android:name=".PhoneStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
Also declare uses-permission ...
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
The broadcast receiver class ...
package x.y;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class PhoneStateBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}
}
And one class to customize phone state listener...
package x.y;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class CustomPhoneStateListener extends PhoneStateListener {
//private static final String TAG = "PhoneStateChanged";
Context context; //Context to make Toast if required
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//when Idle i.e no call
Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//when Off hook i.e in call
//Make intent and start your service here
Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
//when Ringing
Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
How to know if i am successfully connected to my outgoing number in Android?
You can use the PhoneStateLisenter to listen out for changes in the call state.
So you listen for the LISTEN_CALL_STATE change.
With the onCallStateChanged method.
So when the state changes from RINGING to OFFHOOK you know a phone call has been connected
Same principle applies for IDLE to OFFHOOK in your situation, listen out to the change from IDLE to OFFHOOK and you'll know your connected to a call.
You can also look into the ACTION_NEW_OUTGOING_CALL intent, Android allows you to "trap" this when a call is dialled and you may be able to get a result code back stating whether the call was connected or not.
For DTMF tones look into ToneGenerator, this allows you to create DTMF tones and .startTone() should allow you to play the DTMF tone then.
However you cant send DTMF tones through the uplink.
How to detect that the call which is done from our android device is answered or rejceted?
I think you can check outgoing call time of last call in PhoneStateListener
class' onCallStateChanged
method. Fetch the data if state is idle that is TelephonyManager.CALL_STATE_IDLE
.
Something like this:
Cursor mCallCursor = context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null,null,null,null);
int duration = mCallCursor.getColumnIndex( CallLog.Calls.DURATION);
while(mCallCursor.moveToFirst())
{
Toast.makeText(context, mCallCursor.getString(duration), Toast.LENGTH_LONG).show();
}
You can find more about that here. I haven't tested the above code. But something like that should work.
You can check if time's 00:00, then call next number of loop. Else you can stop calling.
Hope this helps you.
Show the information in the middle of the call
check this stack overflow answer.In that answer you can see a toast showing different call states.Instead of that toast make a custom toast and show your updates via that Custom toast.
if you want show an activity instead of toast try this code in your CustomPhoneStateListener
public class CustomPhoneStateListener extends PhoneStateListener {
ActivityManager activityManager;
Intent i1;
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
i1 = new Intent(context, TelephoneyWithoutToastActivity.class);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//when Idle i.e no call
Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//when Off hook i.e in call
//Make intent and start your service here
Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
ActivityManager localActivityManager = (ActivityManager) this.context.getSystemService("activity");
for (String str = ((ActivityManager.RunningTaskInfo) localActivityManager.getRunningTasks(1).get(0)).topActivity.flattenToString();; str = ((ActivityManager.RunningTaskInfo) localActivityManager.getRunningTasks(1).get(0)).topActivity.flattenToString()) {
if ((!str.contains("com.android.phone.InCallScreen")))
continue;
Log.d("IncomingCallPlus", "*****************************************************");
context.startActivity(i1);
return;
}
default:
break;
}
}
}
add this to your activity for activating touch on the default calling screen.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
this function will give touch on both caller screen and popup
public void addInvitePopup(final String number, Context c) {
//check if pref is ok with invite in call
// if(!Preferences.getInstance(c.getInviteInCall())){return ; }
// sets the WindowManager
WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.x = 250;
params.height = LayoutParams.WRAP_CONTENT;
params.width = LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.TRANSLUCENT;
final Context ct = c;
params.gravity = Gravity.TOP;
params.setTitle("Testing");
LinearLayout ly = new LinearLayout(c);
ly.setOrientation(LinearLayout.VERTICAL);
Button inviteButton = new Button(c);
inviteButton.setClickable(true);
inviteButton.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.ic_launcher));
inviteButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "adding to blacklist..", Toast.LENGTH_LONG).show();
v.setBackgroundDrawable(ct.getResources().getDrawable(R.drawable.images));
v.setClickable(false);
// sendMessage(v, number);
//Track this event:
//MixPanelTracking.setPropKeyValue(getApplicationContext(), null, null, "Add friend - During Call");
}
});
inviteButton.setWidth(30);
inviteButton.setHeight(30);
// inviteButton.setLayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
// WindowManager.LayoutParams.WRAP_CONTENT);
ly.addView(inviteButton);
wm.addView(ly, params);
// wm.addView( inviteButton, params);
Log.i("TTT", "after add view");
}
add this permission in manifest file
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
How to know if a call has been established?
listen to the broadcast ACTION_PHONE_STATE_CHANGED, for more information check this out here
Related Topics
Can Not Find the Tag Library Descriptor for "Http://Java.Sun.Com/Jsp/Jstl/Core"
Displaying a Histogram of Image Data
How to Capitalize the First Letter of a String in Java
Can CSS in Javafx Be Used for Animations
How to Change Color of Hamburger Icon in Material Design Navigation Drawer
Full Screen Videoview Without Stretching the Video
Android Studio Convert Iso String to "America/New_York" When Adding to Event to Calendar
Source Code Does Not Match the Bytecode' When Debugging on a Device
How to Hash a String in Android
Java - Ignore Expired Ssl Certificate
Java Output Formatting for Strings
Java: Why Do You Need to Specify an 'F' in a Float Literal
Find First Element by Predicate
Android - How to Take Screenshot Programmatically
Change Position of Google Maps API's "My Location" Button