Ahmed Flash Developer Ahmed Flash Developer - 1 month ago 4
Android Question

How to use Context paramater with acceptCall function android?

I have tried to add some the functions to class so I can open incoming calls, but I have a problem occurring in the passing of the parameter "Context"

enter image description here

package com.bitgriff.androidcalls;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.widget.Toast;

public class CallHelper {

private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;
private OutgoingReceiver outgoingReceiver;
public Context context;

private CallHelper(Context context) {

this.context = context.getApplicationContext();

* Listener to detect incoming calls.

public CallHelper(Context ctx) {
this.ctx = ctx;
callStateListener = new CallStateListener();
outgoingReceiver = new OutgoingReceiver();

private class CallStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// called when someone is ringing to this phone
//Not work
acceptCall(Context ctx);
Toast.makeText(ctx,"Incoming: "+incomingNumber,Toast.LENGTH_LONG).show();

public void acceptCall(Context context){
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));

private void rejectCall(Context context)
Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON); buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");
* Broadcast receiver to detect the outgoing calls.
public class OutgoingReceiver extends BroadcastReceiver {
public OutgoingReceiver() {

public void onReceive(Context context, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

"Outgoing: "+number,


* Start calls detection.
public void start() {
tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
ctx.registerReceiver(outgoingReceiver, intentFilter);

* Stop calls detection.
public void stop() {
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);



Either don't put Context


Or cast it

acceptCall((Context) ctx);

This is not valid syntax.

acceptCall(Context ctx);