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)
}
}
}
3条答案
按热度按时间6tqwzwtp1#
您可以使用此代码段向现有呼叫日志内容提供程序添加新记录:
(Code Google Voice Callback for Android)
记住在清单中添加权限
u3r8eeie2#
链接的帖子解释得很好,所以我不知道你为什么又问了一遍。您不能修改通话记录,除非您保留自己的数据库或固件。
7lrncoxx3#
我准备了一个Kotlin版本,它添加了随机项目,并具有不同的类型:
清单
主活动注:我没有处理这里的权限授予。我认为他们被授予。
虽然文档有点奇怪,因为它说它支持3种类型,但它列出了5种类型:
https://developer.android.com/reference/android/provider/CallLog.Calls#TYPE
由于某种原因,我在尝试使用VOICEMAIL_TYPE时遇到了一个异常:
java.lang.IllegalArgumentException:URI内容://call_log/calls不能用于语音邮件记录。请在uri中设置“allow_voicemails=true”。
此外,当尝试使用BLOCKED_TYPE时,我无法在Phone应用程序中的任何位置看到该项目。
如果有人能告诉我如何修理它,请告诉我。