gpt4 book ai didi

android - Android Kotlin-每分钟发送一次通知

转载 作者:行者123 更新时间:2023-12-02 13:39:18 24 4
gpt4 key购买 nike

我正在尝试每分钟向用户发送一次通知(仅出于测试目的)。这是我到目前为止所拥有的:

class AlertReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent){
sendNotification(context, "Sample", "Notification sent....")
}

fun sendNotification(context: Context, title: String, body: String){
val notification = NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_icon_background)
.setContentTitle(title)
.setContentText(body)

val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, Intent(context, TimerService::class.java), 0)

notification.setContentIntent(pendingIntent)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1, notification.build())
}
}

class TimerService : Service() {

override fun onCreate(){
toast("Activity Created")
setAlarm()
super.onCreate()
}

override fun onDestroy(){
super.onDestroy()
toast("Stopped activity")
}

fun setAlarm(){

val alertTime: Long = GregorianCalendar().timeInMillis+5*1000
val alertIntent: Intent = Intent(this, AlertReceiver::class.java)

val manager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
manager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT))
}

override fun onBind(intent: Intent): IBinder? {
return null
}
}

这是我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.luciddream">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TimerActivity">
<intent-filter>
<action android:name="com.example.user.luciddream.TimerActivity" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service android:name=".TimerService"></service>
<receiver android:name=".AlertReceiver"></receiver>
</application>

</manifest>

使用 manager.set可以正常工作。但是通过这种方式,显然它将只等待一次并最终发出通知。但是,我似乎无法使用 manager.setRepeating()使它正常工作。

在这种情况下, manager.setRepeating()代码应如何显示?

谢谢

最佳答案

您可以做下面的事情:

定义NotificationUtil.kt以为Android O及以上版本创建Channels:

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.ContextWrapper
import android.graphics.Color
import android.os.Build
import android.support.annotation.RequiresApi

@RequiresApi(Build.VERSION_CODES.O)
class NotificationUtils(base: Context) : ContextWrapper(base) {

val nManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

init {
createChannels()
}

@RequiresApi(Build.VERSION_CODES.O)
private fun createChannels() {
val myChannel = NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT).apply {
enableLights(true)
enableVibration(true)
lightColor = Color.GREEN
lockscreenVisibility = Notification.VISIBILITY_PRIVATE
}

nManager.createNotificationChannel(myChannel)
}

companion object {
const val CHANNEL_ID = "my.app.CHANNEL_ID"
const val CHANNEL_NAME = "my.app.Notification"
}
}

创建 notificationService.kt来处理通知:
import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.media.RingtoneManager
import android.os.Build
import android.os.IBinder
import oryx.kortex.locateme.R
import android.provider.Telephony



class NotificationService : Service() {
override fun onBind(intent: Intent?): IBinder? {
return null
}

override fun onCreate() {
val context = this as Context
}

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {

val defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this)
val intent = Intent(Intent.ACTION_MAIN)
intent.`package` = defaultSmsPackageName

val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)

val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

val mNotificationId: Int = 1000

val nManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val mNotification = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder(context, CHANNEL_ID)
} else {
Notification.Builder(context)
}.apply {
setContentIntent(pendingIntent)
setSmallIcon(R.drawable.ic_error_black_24dp)
setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher))
setAutoCancel(true)
setContentTitle("Title")
setStyle(Notification.BigTextStyle().bigText("message"))
setContentText("MESSAGE")
}.build()

nManager.notify(mNotificationId, mNotification)
return Service.START_STICKY
}

companion object {
const val CHANNEL_ID = "my.app.CHANNEL_ID"
}
}

确保在 MainActivity.kt中调用Util来创建 channel :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)  NotificationUtils(this)

在broadCastRciever中,定义服务并调用它:
val notificationService = Intent(context, NotificationService::class.java)

context.startService(notificationService)

确保在 manifest中定义必需的:
<service android:name=".Services.NotificationService" />
<receiver android:name=".broadcasts.xxxx">
<intent-filter>
<action android:name="android.intent.action.xxx" />
</intent-filter>
</receiver>

关于android - Android Kotlin-每分钟发送一次通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44763202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com