有没有可能写入通话记录数据库?我的意思是我想将选定的号码添加到通话历史记录中。我尝试在网络上搜索教程,但找不到任何...虽然有一件事是肯定的,它可以完成。因为我在网上看到了很多应用程序,可以恢复通话记录,所以我猜我们可以以某种方式修改通话历史记录数据库(但我不清楚具体方法)。
我已经阅读了这篇帖子,但它发表的时间很久以前。
任何帮助都将不胜感激!谢谢!
我已经阅读了这篇帖子,但它发表的时间很久以前。
任何帮助都将不胜感激!谢谢!
您可以使用此代码片段向现有通话记录内容提供程序添加新记录:
public static void insertPlaceholderCall(ContentResolver contentResolver, String number){
ContentValues values = new ContentValues();
values.put(CallLog.Calls.NUMBER, number);
values.put(CallLog.Calls.DATE, System.currentTimeMillis());
values.put(CallLog.Calls.DURATION, 0);
values.put(CallLog.Calls.TYPE, CallLog.Calls.OUTGOING_TYPE);
values.put(CallLog.Calls.NEW, 1);
values.put(CallLog.Calls.CACHED_NAME, "");
values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
Log.d(TAG, "Inserting call log placeholder for " + number);
contentResolver.insert(CallLog.Calls.CONTENT_URI, values);
}
(代码取自Google Voice Callback for Android)
记得在清单文件中添加权限。
<uses-permission
android:name="android.permission.READ_CONTACTS"/>
<uses-permission
android:name="android.permission.WRITE_CONTACTS"/>
这篇链接的帖子已经很好地解释了,所以我不知道你为什么要再问一遍。除非你保留自己的数据库或自己的固件,否则无法修改通话记录。
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
MainActivity 注意:我在这里没有处理授予权限。我假设它们已经被授予。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<View>(R.id.button).setOnClickListener {
thread {
val itemsToAdd = 2000
val phoneListSize = 10
val phonesList = ArrayList<String>(phoneListSize)
//Israeli cell phone prefix
val phonePrefix = "+97250"
val extraDigitsAfterPhonePrefix = 7
for (i in 0 until phoneListSize) {
val sb = StringBuilder(phonePrefix.length + 9)
sb.append(phonePrefix)
for (j in 0 until extraDigitsAfterPhonePrefix)
sb.append(Random.nextInt(10).toString())
phonesList.add(sb.toString())
}
Log.d("AppLog", "list of phones to use:${phonesList}")
try {
for (i in 0 until itemsToAdd) {
val number = phonesList.get(Random.nextInt(phonesList.size))
Log.d("AppLog", "$i - Inserting call log placeholder for $number")
insertPlaceholderCall(contentResolver, number)
}
} catch (e: Throwable) {
Log.e("AppLog", "failed:$e")
e.printStackTrace()
}
Log.d("AppLog", "done")
}
}
}
companion object {
fun insertPlaceholderCall(contentResolver: ContentResolver, number: String) {
val values = ContentValues()
values.put(CallLog.Calls.NUMBER, number)
val cal = Calendar.getInstance()
//go back up to 30 days, in seconds
cal.add(Calendar.SECOND, -Random.nextInt(30 * 24 * 60 * 1000))
values.put(CallLog.Calls.DATE, cal.timeInMillis)
// up to 10 minutes call
values.put(CallLog.Calls.DURATION, Random.nextInt(60 * 10))
val type = when (Random.nextInt(4)) {
0 -> CallLog.Calls.OUTGOING_TYPE
1 -> CallLog.Calls.INCOMING_TYPE
2 -> CallLog.Calls.MISSED_TYPE
3 -> CallLog.Calls.REJECTED_TYPE
//BLOCKED_TYPE is ignored in the Phone app, for some reason
4 -> CallLog.Calls.BLOCKED_TYPE
//VOICEMAIL_TYPE causes an exception of IllegalArgumentException: Uri content://call_log/calls cannot be used for voicemail record. Please set 'allow_voicemails=true' in the uri.
else -> CallLog.Calls.VOICEMAIL_TYPE
}
values.put(CallLog.Calls.TYPE, type)
values.put(CallLog.Calls.NEW, 1)
values.put(CallLog.Calls.CACHED_NAME, "")
values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0)
values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "")
contentResolver.insert(CallLog.Calls.CONTENT_URI, values)
}
}
}
https://developer.android.com/reference/android/provider/CallLog.Calls#TYPE
出于某种原因,当我尝试使用VOICEMAIL_TYPE时,出现了一个异常: