我是Android开发的初学者。我想确保“OUTGOING”来自我的应用程序,而不是 Android 中的设备调用应用程序。我如何得到这个?意味着如果有人从我的应用程序呼叫,它将重定向到呼叫应用程序的设备的呼叫屏幕,当呼叫结束时,它将重定向回我的应用程序。如果有人从我的应用程序呼叫,我希望它显示呼叫是来自我的应用程序而不是设备应用程序。
我利用 CallLog.Calls 来确定呼叫的 TYPE。另外,我发现当我按下“呼叫”按钮时,会有一些毫秒的变化。那么,有什么办法可以确定这个“拨出”电话是否来自我的应用程序?
这是我的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();
}
}
我想在我的应用程序中包含最近的通话,并表明它们来自我的应用程序。
我感谢您对此的任何指导。
不可以,因为除非您在已取得 root 权限的设备上运行或者是 OEM 或 TelephonyService,否则您的应用程序不会发出任何呼叫。 相反,您启动拨号器应用程序,它实际上会拨打电话。 因此,所有呼叫均来自拨号器应用程序。 您的应用程序请求拨号器应用程序的事实不会在呼叫日志中捕获。 即使日志中有一个位置(没有),也不能保证任何特定的拨号器应用程序实际上会填充该字段。