since API 23 is possible, see Replacing the default Phone app on Android 6 and 7 with InCallService arekolek .
Kotlin Version: Simple Phone
Java Version: Simple Phone Dialer
EDIT
As recommended, the following is the simplest version of Java.
The following is a manifest with the necessary intent-filter .
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.aliton.customphonecall"> <uses-permission android:name="android.permission.CALL_PHONE" /> <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/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".DialerActivity" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.DIAL" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="tel" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.DIAL" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <service android:name=".CallService" android:permission="android.permission.BIND_INCALL_SERVICE"> <meta-data android:name="android.telecom.IN_CALL_SERVICE_UI" android:value="true" /> <intent-filter> <action android:name="android.telecom.InCallService" /> </intent-filter> </service> <activity android:name=".CallActivity"></activity> </application> </manifest>
MainActivity just has a Button with an Intent redirecting to DialerActivity .
Below DialerActivity . In this exercise, you will install the default application to make calls using the user interface.
public class DialerActivity extends AppCompatActivity { private static final int REQUEST_CALL_PHONE = 10; EditText phoneNumber; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dialer); phoneNumber = (EditText) findViewById(R.id.etNumber); Button bCall = (Button) findViewById(R.id.btnCall); offerReplacingDefaultDialer(); bCall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { makeCall(); } }); } private void makeCall() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { Uri uri = Uri.parse("tel:"+phoneNumber.getText().toString().trim()); Intent Call = new Intent(Intent.ACTION_CALL, uri);
This is how InCallService implemented to manage calls.
public class CallService extends InCallService { OngoingCallObject ongoingCallObject; @Override public void onCallAdded(Call call) { super.onCallAdded(call); new OngoingCallObject().setCall(call);
Here's how the object is implemented.
public class OngoingCallObject { private static Call call; private Object callback = new Callback() { @Override public void onStateChanged(Call call, int state) { super.onStateChanged(call, state); Log.i("debinf OngoingObj", "state is "+state); } }; public void setCall(Call call) { if (this.call != null) { this.call.unregisterCallback((Call.Callback)callback); } if (call != null) { call.registerCallback((Call.Callback)callback); Log.i("debinf OngoingObj", "call.getState() is "+call.getState()); } this.call = call; } public void answer() {
And finally, CallActivity where you launch your user interface.
public class CallActivity extends AppCompatActivity { TextView callInfo; Button answer, hangup; private String number; private OngoingCallObject ongoingCall; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_call); ongoingCall = new OngoingCallObject(); answer = (Button) findViewById(R.id.answer); hangup = (Button) findViewById(R.id.hangup); callInfo = (TextView) findViewById(R.id.callInfo); number = Objects.requireNonNull(getIntent().getData().getSchemeSpecificPart()); callInfo.setText("Calling number : "+number); answer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
Here is the XML for DialerActivity : activity_dialer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".DialerActivity"> <EditText android:id="@+id/etNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Tel number"/> <Button android:id="@+id/btnCall" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="CallActivity" android:layout_below="@+id/etNumber" /> </RelativeLayout>
Here is the XML for CallActivity : activity_call.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".CallActivity"> <TextView android:id="@+id/callInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.3" tools:text="Hello World!" /> <Button android:id="@+id/answer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Answer" app:layout_constraintBaseline_toBaselineOf="@+id/hangup" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/hangup" /> <Button android:id="@+id/hangup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hang up" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/answer" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/callInfo" /> </android.support.constraint.ConstraintLayout>
I hope this helps!