Creating Custom Incoming Call Screen Android 5.1.1

Viewed 154

I am creating a calling application using TelephonyManager for getting state, BroadcastReceiver to notify the call and ITelephony for implementation.

-> I am able to answer and reject calls, But the problem is I cant hear caller voice in my device

Note : I have a rooted device and using android LOLLIPOP.

-> My app answers the incoming call properly after answering the call I am indenting to another activity to change the layout But, the intent is also not happening and I am not able to hear callers voice.

UPDATE : I have used handlers to update UI it is working very fine. If anyone have solution for getting caller audio please help me :)

Here is my code.

MainActivity.java :

public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CALL = 1;
public String number1 = "";
private Button close;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    close = findViewById(R.id.close);

    if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.MODIFY_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_CALL);
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.MODIFY_PHONE_STATE}, REQUEST_CALL);
    }

    close.setOnClickListener(v -> finishAndRemoveTask());

}}

call.java : (My BroadcastReceiver)

public class call extends BroadcastReceiver {

public String number1 = "";

@SuppressLint("UnsafeProtectedBroadcastReceiver")
@Override
public void onReceive(Context context, Intent intent) {

    number1 = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE)
            .equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        showToast(context, "Incoming call");
        Intent in = new Intent(context, inComingCallActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        in.putExtra("number", number1);
        context.startActivity(in);
        Log.d("inCall", number1);
    } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE)
            .equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
        showToast(context, "Call on going");
        Intent in = new Intent(context, offHookActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        in.putExtra("number", number1);
        context.startActivity(in);
    } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE)
            .equals(TelephonyManager.EXTRA_STATE_IDLE)) {
        showToast(context, "Call ended");
    }
}

public void showToast(Context context, String string) {

    Toast.makeText(context, string, Toast.LENGTH_SHORT).show();

}}

inComingCallActivity.java :

public class inComingCallActivity extends AppCompatActivity {

TextView incoming;
ImageButton answer, reject;
MainActivity c = new MainActivity();
TelephonyManager tm;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_in_coming_call);

    incoming = findViewById(R.id.callDetails);
    answer = findViewById(R.id.answerCall);
    reject = findViewById(R.id.rejectCall);

    Bundle extras = getIntent().getExtras();
    String IncomingNumber = extras.getString("number");
    System.out.println("inCall : " + IncomingNumber);
    Log.d("main", c.number1);
    incoming.setText(IncomingNumber);

    answer.setOnClickListener(view -> {

       Toast.makeText(this, "Answering call", Toast.LENGTH_SHORT).show();
        activity.runOnUiThread(() -> {
            Intent in = new Intent(getApplicationContext(), offHookActivity.class);
            in.putExtra("number", IncomingNumber);
            Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        answerCall(getApplicationContext());
                        sleep(1000);
                       audioManager.setMode(AudioManager.MODE_IN_CALL);
                       if (!audioManager.isSpeakerphoneOn())
                           audioManager.setSpeakerphoneOn(true);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };

            thread.start();
            startActivity(in);
        });
      
        finishAndRemoveTask();
    });

    reject.setOnClickListener(view -> {

        Toast.makeText(this, "Ending call", Toast.LENGTH_SHORT).show();
        disconnectCall(getApplicationContext());
        finishAndRemoveTask();
    });

}

@SuppressLint("SoonBlockedPrivateApi")
public void answerCall(Context context) {
    tm = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
    Method m1 = null;
    try {
        m1 = tm.getClass().getDeclaredMethod("getITelephony");
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    assert m1 != null;
    m1.setAccessible(true);
    Object iTelephony = null;
    try {
        iTelephony = m1.invoke(tm);
    } catch (IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }

    Method m3 = null;
    try {
        assert iTelephony != null;
        m3 = iTelephony.getClass().getDeclaredMethod("answerRingingCall");
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }

    try {
        assert m3 != null;
        m3.invoke(iTelephony);
    } catch (IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }
}

@SuppressLint("SoonBlockedPrivateApi")
public void disconnectCall(Context context) {

    tm = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
    Method m1 = null;
    try {
        m1 = tm.getClass().getDeclaredMethod("getITelephony");
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    assert m1 != null;
    m1.setAccessible(true);
    Object iTelephony = null;
    try {
        iTelephony = m1.invoke(tm);
    } catch (IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }

    Method m3 = null;
    try {
        assert iTelephony != null;
        m3 = iTelephony.getClass().getDeclaredMethod("endCall");
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }

    try {
        assert m3 != null;
        m3.invoke(iTelephony);
    } catch (IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }
}

}

offHookActivity.java :

public class offHookActivity extends AppCompatActivity {

TextView onGoing;
ImageButton turnOffCall;
MainActivity c = new MainActivity();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_off_hook);
    String number = c.number1;
    onGoing = findViewById(R.id.ohCallDetails);
    turnOffCall = findViewById(R.id.ohTurnOffCallCall);

    Bundle extras = getIntent().getExtras();
    String IncomingNumber = extras.getString("number");
    System.out.println("OffHook : " + IncomingNumber);
    Log.d("OffHook", number);
    onGoing.setText(IncomingNumber);

    turnOffCall.setOnClickListener(v -> finishAndRemoveTask());

}}

ManiFest.XML :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.callapp">

<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.CallApp">
    <activity android:name=".offHookActivity"></activity>
    <activity android:name=".inComingCallActivity" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".call"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

So, I need help for why I am not able to hear callers voice and intent is not happening after accepting the call.

I have provieded my enter code any help will be greatful, Thanks in advance.

0 Answers
Related