Commit adc54b18 by zhangchengbo

fix:添加应用icon角标功能

parent 504b0350
......@@ -2,15 +2,9 @@ package com.secspace.sms.activities
import android.annotation.SuppressLint
import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.role.RoleManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ShortcutManager
import android.graphics.Color
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.os.Bundle
......@@ -19,7 +13,6 @@ import android.text.TextUtils
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.app.NotificationCompat
import androidx.lifecycle.lifecycleScope
import com.secspace.sms.R
import com.secspace.sms.adapters.ConversationsAdapter
......@@ -88,88 +81,6 @@ class MainActivity : SimpleActivity() {
return
}
// testSendNotify()
// setBadgeNum(99)
// setBadgeCount(this@MainActivity,66)
}
//测试通知
private fun testSendNotify() {
val builder: NotificationCompat.Builder
//8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = NotificationCompat.Builder(this, getChannelId())
builder.setSmallIcon(com.secspace.sms.R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_ALL)
.setTicker("title")
.setAutoCancel(true)
.setContentTitle("contentTitle")
.setContentText("contentText")
.setNumber(99)
notificationManager.notify(123, builder.build())
}
}
/**
* https://blog.51cto.com/u_16213326/11294652
*/
private fun setBadgeCount(context: Context, count: Int) {
val intent = Intent("android.intent.action.BADGE_COUNT_UPDATE")
intent.putExtra("badge_count", count)
intent.putExtra("badge_count_package_name", context.packageName)
intent.putExtra("badge_count_class_name", getLauncherClassName(context))
context.sendBroadcast(intent)
}
private fun getLauncherClassName(context: Context): String? {
val pm = context.packageManager
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val resolveInfos = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
for (resolveInfo in resolveInfos) {
val pkgName = resolveInfo.activityInfo.applicationInfo.packageName
if (pkgName.equals(context.packageName, ignoreCase = true)) {
return resolveInfo.activityInfo.name
}
}
return null
}
//https://developer.honor.com/cn/forum/topicdetail/topicid-3540765949296640
private fun setBadgeNum(num: Int) {
try {
val bunlde = Bundle()
bunlde.putString("package", "com.test.badge") // com.test.badge is your package name
bunlde.putString("class", "com.test. badge.MainActivity") // com.test. badge.MainActivity is your apk main activity
bunlde.putInt("badgenumber", num)
this.contentResolver.call(Uri.parse("content://com.hihonor.android.launcher.settings/badge/"), "change_badge", null, bunlde)
} catch (e: java.lang.Exception) {
// mIsSupportedBade = false
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
fun getChannelId(): String {
val channelId = "1"
val channelName = "sms"
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
channel.enableLights(true) //显示桌面红点
channel.lightColor = Color.RED
channel.setShowBadge(true)
notificationManager.createNotificationChannel(channel)
return channel.id
}
@RequiresApi(Build.VERSION_CODES.N_MR1)
fun getShortcutCount(context: Context): Int {
val shortcutManager: ShortcutManager = context.getSystemService(ShortcutManager::class.java)
val shortcutInfoList = shortcutManager.dynamicShortcuts
return shortcutInfoList.size
}
override fun onResume() {
......@@ -407,7 +318,10 @@ class MainActivity : SimpleActivity() {
}
private fun getNewConversations(cachedConversations: ArrayList<Conversation>) {
val privateCursor = getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true)
var privateCursor: Cursor? = null
lifecycleScope.launch(Dispatchers.Main) {
privateCursor = getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true)
}
val privateContacts = MyContactsContentProvider.getSimpleContacts(this@MainActivity, privateCursor)
// Log.e(TAG, " getNewConversations privateContacts:${GsonUtil.parseListToJson(privateContacts)}")
......@@ -463,11 +377,13 @@ class MainActivity : SimpleActivity() {
}
val allConversations = conversationsDB.getNonArchived() as ArrayList<Conversation>
// Log.e(TAG, "----MainActivity----handleMessage allConversations:${GsonUtil.parseListToJson(allConversations)}")
// runOnUiThread {
setupConversations(allConversations)
// }
// Log.e(TAG, "----MainActivity----handleMessage appRunCount:${config.appRunCount}")
val readResult = conversations.count { !it.read }
lifecycleScope.launch(Dispatchers.Main) {
setBadgeNum(readResult)
}
if (config.appRunCount == 1) {
conversations.map { it.threadId }.forEach { threadId ->
val messages = getMessages(threadId, getImageResolutions = false, includeScheduledMessages = false)
......@@ -600,6 +516,19 @@ class MainActivity : SimpleActivity() {
}.start()
}
//https://developer.honor.com/cn/forum/topicdetail/topicid-3540765949296640
private fun setBadgeNum(num: Int) {
try {
val bunlde = Bundle()
bunlde.putString("package", "com.secspace.sms") // com.test.badge is your package name
bunlde.putString("class", "com.secspace.sms.activities.SplashActivity.Orange") // com.test. badge.MainActivity is your apk main activity
bunlde.putInt("badgenumber", num)
this.contentResolver.call(Uri.parse("content://com.hihonor.android.launcher.settings/badge/"), "change_badge", null, bunlde)
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
}
@SuppressLint("NotifyDataSetChanged")
private fun notifyDatasetChanged() {
getOrCreateConversationsAdapter().notifyDataSetChanged()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment