Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
Simple-Sms
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhangchengbo
Simple-Sms
Commits
ed0f930f
Commit
ed0f930f
authored
Aug 06, 2025
by
zhangchengbo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:过滤重复插入数据库Message
parent
f19e250f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
15 deletions
+18
-15
SmsSendObserver.kt
...main/kotlin/com/secspace/sms/receivers/SmsSendObserver.kt
+18
-15
No files found.
app/src/main/kotlin/com/secspace/sms/receivers/SmsSendObserver.kt
View file @
ed0f930f
...
...
@@ -13,12 +13,14 @@ import com.secspace.sms.extensions.*
import
com.secspace.sms.helpers.refreshMessages
import
com.secspace.sms.models.Conversation
import
com.secspace.sms.models.Message
import
com.simplemobiletools.commons.extensions.*
import
com.simplemobiletools.commons.extensions.getLongValue
import
com.simplemobiletools.commons.extensions.getMyContactsCursor
import
com.simplemobiletools.commons.extensions.getStringValue
import
com.simplemobiletools.commons.extensions.isNumberBlocked
import
com.simplemobiletools.commons.helpers.SimpleContactsHelper
import
com.simplemobiletools.commons.helpers.ensureBackgroundThread
import
com.simplemobiletools.commons.models.PhoneNumber
import
com.simplemobiletools.commons.models.SimpleContact
import
com.simplemobiletools.commons.util.GsonUtil
class
SmsSendObserver
(
private
val
context
:
Context
,
handler
:
Handler
?)
:
ContentObserver
(
handler
)
{
private
val
TAG
=
"SmsObserver"
...
...
@@ -30,29 +32,35 @@ class SmsSendObserver(private val context: Context, handler: Handler?) : Content
override
fun
onChange
(
selfChange
:
Boolean
)
{
super
.
onChange
(
selfChange
)
Log
.
d
(
TAG
,
"SmsObserver: onChange "
)
val
cursor
=
context
.
contentResolver
.
query
(
Uri
.
parse
(
"content://sms/sent"
),
SMS_PROJECTION
,
" type=?"
,
arrayOf
(
"2"
),
// "date DESC LIMIT 1"
sortOrder
)
if
(
cursor
==
null
)
return
if
(
cursor
.
moveToFirst
())
{
val
body
=
cursor
.
getStringValue
(
Sms
.
BODY
)
val
address
=
cursor
.
getString
(
cursor
.
getColumnIndex
(
"address"
)).
toString
()
val
date
=
/*System.currentTimeMillis()*/
cursor
.
getString
(
cursor
.
getColumnIndex
(
"date"
)).
toLong
()
val
address
=
cursor
.
getString
Value
(
Sms
.
ADDRESS
)
//cursor.getString
(cursor.getColumnIndex("address")).toString()
val
date
=
cursor
.
getLongValue
(
Sms
.
DATE
)
//
cursor.getString(cursor.getColumnIndex("date")).toLong()
val
threadId
=
context
.
getThreadId
(
address
)
com
.
secspace
.
log
.
Log
.
d
(
TAG
,
"获取当前日期 threadId = $threadId"
)
// val dateTime = cursor.getStringValue(Sms.DATE)
// Log.d(TAG, "SmsObserver: dateTime = $dateTime ")
// Log.d(TAG, "SmsObserver: address = $address threadIds = $threadId body = $body ")
ensureBackgroundThread
{
val
listContainsDate
:
List
<
Int
>
=
context
.
messagesDB
.
getAll
().
map
{
it
.
date
}.
distinct
()
val
convDate
=
(
date
/
1000
).
toInt
()
// com.secspace.log.Log.d(TAG, "获取当前日期 convDate = $convDate")
// com.secspace.log.Log.d(TAG, "获取数据库Date集合 isContains = ${listContainsDate.contains(convDate)} listContainsDate = $listContainsDate")
handleMessage
(
context
,
address
,
body
,
date
,
threadId
,
Telephony
.
Sms
.
MESSAGE_TYPE_SENT
,
0
,
0
)
if
(!
listContainsDate
.
contains
(
convDate
)
||
listContainsDate
.
isEmpty
())
{
handleMessage
(
context
,
address
,
body
,
date
,
threadId
,
Telephony
.
Sms
.
MESSAGE_TYPE_SENT
,
0
,
0
)
}
cursor
.
close
()
}
}
cursor
.
close
()
}
...
...
@@ -71,15 +79,11 @@ class SmsSendObserver(private val context: Context, handler: Handler?) : Content
if
(!
context
.
isNumberBlocked
(
address
))
{
val
privateCursor
=
context
.
getMyContactsCursor
(
favoritesOnly
=
false
,
withPhoneNumbersOnly
=
true
)
ensureBackgroundThread
{
val
conversation
:
Conversation
=
context
.
getConversations
(
threadId
).
firstOrNull
()
?:
return
@ensureBackgroundThread
// Log.d(TAG, "SmsObserver handleMessage conversation:${GsonUtil.parseBeanToJson(conversation)}")
try
{
context
.
insertOrUpdateConversation
(
conversation
)
}
catch
(
ignored
:
Exception
)
{
}
val
senderName
=
context
.
getNameFromAddress
(
address
,
privateCursor
)
val
phoneNumber
=
PhoneNumber
(
address
,
0
,
""
,
address
)
...
...
@@ -93,7 +97,6 @@ class SmsSendObserver(private val context: Context, handler: Handler?) : Content
newMessageId
=
msgResult
.
last
().
id
newMessageId
++
}
// Log.d(TAG, "SmsObserver handleMessage newMessageId:$newMessageId")
val
message
=
Message
(
newMessageId
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment