I'm a beginner at Android development. I want to ensure that 'OUTGOING' is from my app rather than the device calling app in Android. How do I get this? means if someone calls from my app, it will redirect to the device calling app's call screen, and when the call ends, it will redirect back to my app. If someone call from my app, I want it to show that the call is from my app rather than the device app.
I utilized CallLog.Calls to determine the TYPE of the calls. Also, I found that when I push the Call button, some little milliseconds vary. So, is there any way to determine whether this 'OUTGOING' call is from my app?
this is my MainActivity.java:
package com.example.calllogsapp;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
EditText phoneNo;
FloatingActionButton callbtn;
static int PERMISSION_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
Log.d("MainActivity", "This is a debug message");
phoneNo = findViewById(R.id.editTextPhone);
callbtn = findViewById(R.id.callbtn);
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},PERMISSION_CODE);
}
callbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Print before phone number
long time= System.currentTimeMillis();
SimpleDateFormat milliFormat = new SimpleDateFormat("HH:mm:ss:SSS");
String currentBeforePhNo = milliFormat.format(time);
//PhoneNumber
String phoneno = phoneNo.getText().toString();
//Intent
Intent i = new Intent(Intent.ACTION_CALL);
//Telephone
i.setData(Uri.parse("tel:"+phoneno));
//Start Activity
startActivity(i);
//Print After Start Activity
String afterStart = milliFormat.format(time);
System.out.println("After Start Activity:"+afterStart);
Log.v("After Start Activity:", afterStart);
}
});
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_CALL_LOG)){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CALL_LOG}, 1);
}else{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CALL_LOG}, 1);
}
}else {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(getCallDetails());
}
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "No Permission Granted!", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
private String getCallDetails(){
StringBuffer sb = new StringBuffer();
Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null , null, android.provider.CallLog.Calls.DATE + " DESC", null);
int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details:\n\n");
while(managedCursor.moveToNext()){
String phnumber = managedCursor.getString(number);
String cname = managedCursor.getString(name);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
SimpleDateFormat newformatter = new SimpleDateFormat("dd-MM-yy || HH:mm:ss:SSS");
String dateString = newformatter.format(callDayTime);
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode){
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append("\nName: "+ cname +"\nPhone Number: "+ phnumber + "\nCall Type: "+ dir + "\nCall Date: "+ dateString + "\nCall Duration: "+ callDuration);
sb.append("\n--------------------------------------");
}
managedCursor.close();
return sb.toString();
}
}
I want to include recent calls in my app and indicate that they are from my app.
I'm thankful for any guidance regarding this.
No, because unless you're running on a rooted device or are an OEM or are a TelephonyService, no calls are from your app. Instead you launch the dialer app, which actually makes the call. So all calls are from the dialer app. The fact that the dialer app was requested by your app is not captured in the call logs. Even if there was a place in the logs for it to be (there isn't), there'd be no promise that any particular dialer app actually populates that field.