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
3ff54263
Commit
3ff54263
authored
Aug 05, 2025
by
zhangchengbo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:添加SMS数据库变化监听Observe,针对顺丰小哥app内发送短信
parent
cc8adc07
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
124 additions
and
0 deletions
+124
-0
SmsSendObserver.kt
...main/kotlin/com/secspace/sms/receivers/SmsSendObserver.kt
+124
-0
No files found.
app/src/main/kotlin/com/secspace/sms/receivers/SmsSendObserver.kt
0 → 100644
View file @
3ff54263
package
com.secspace.sms.receivers
import
android.annotation.SuppressLint
import
android.content.Context
import
android.database.ContentObserver
import
android.net.Uri
import
android.os.Handler
import
android.os.Looper
import
android.provider.Telephony
import
android.provider.Telephony.Sms
import
android.util.Log
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.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"
private
val
SMS_PROJECTION
=
arrayOf
(
"address"
,
"person"
,
"date"
,
"type"
,
"body"
)
private
val
sortOrder
=
"${Sms.DATE} DESC LIMIT 1"
@SuppressLint
(
"Range"
)
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
threadId
=
context
.
getThreadId
(
address
)
// val dateTime = cursor.getStringValue(Sms.DATE)
// Log.d(TAG, "SmsObserver: dateTime = $dateTime ")
// Log.d(TAG, "SmsObserver: address = $address threadIds = $threadId body = $body ")
handleMessage
(
context
,
address
,
body
,
date
,
threadId
,
Telephony
.
Sms
.
MESSAGE_TYPE_SENT
,
0
,
0
)
}
cursor
.
close
()
}
private
fun
handleMessage
(
context
:
Context
,
address
:
String
,
body
:
String
,
date
:
Long
,
threadId
:
Long
,
type
:
Int
,
subscriptionId
:
Int
,
status
:
Int
,
)
{
val
photoUri
=
SimpleContactsHelper
(
context
).
getPhotoUriFromPhoneNumber
(
address
)
Handler
(
Looper
.
getMainLooper
()).
post
{
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
)
val
participant
=
SimpleContact
(
0
,
0
,
senderName
,
photoUri
,
arrayListOf
(
phoneNumber
),
ArrayList
(),
ArrayList
())
val
participants
=
arrayListOf
(
participant
)
val
messageDate
=
(
date
/
1000
).
toInt
()
var
newMessageId
=
0L
val
msgResult
:
List
<
Message
>
=
context
.
messagesDB
.
getAllById
()
if
(
msgResult
.
isNotEmpty
())
{
newMessageId
=
msgResult
.
last
().
id
newMessageId
++
}
// Log.d(TAG, "SmsObserver handleMessage newMessageId:$newMessageId")
val
message
=
Message
(
newMessageId
,
body
,
type
,
status
,
participants
,
messageDate
,
true
,
threadId
,
false
,
null
,
address
,
senderName
,
photoUri
,
subscriptionId
)
// Log.d(TAG, "SmsObserver handleMessage message:${GsonUtil.parseBeanToJson(message)}")
context
.
messagesDB
.
insertOrUpdate
(
message
)
if
(
context
.
config
.
isArchiveAvailable
)
{
context
.
updateConversationArchivedStatus
(
threadId
,
false
)
}
refreshMessages
()
}
}
}
}
}
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