Android Kotlin – 每分钟发送通知
我试图每分钟向用户发送一个通知(仅用于测试目的)。 这是我迄今为止:
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()
代码应该如何查看?
谢谢
尝试下面的代码示例setRepeating
。 看起来你设置的时间间隔是错误的。
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 2*60*1000, pendingIntent);
我会更新我的答案,如果它的工作与否。
- Android Toast无法正常工作(应用程序崩溃)
- Kotlin通用参数在函数中使用
- 在kotlin中使用Interface时获取平台声明冲突
- 如何解决:“错误:无法找到符号类…”转换后的Java类Kotlin?
- 在Kotlin中开发的Android库模块导出到Java应用程序导致失败的解决方案:Lkotlin / jvm / internal / Intrinsics
- Android Kotlin创建类实现Parcelable在writeToParcel方法的“覆盖”中给出一个错误
- 使用编译“org.jetbrains.kotlin:kotlin-stdlib-jre7:$ kotlin_version”给错误
- Kotlin内部类不能访问外面的私人方法?
- Android + Kotlin – 项目不再建设