Created
March 24, 2020 22:10
-
-
Save john-lorrenz/154960750266dd5216d3ba891f152ed1 to your computer and use it in GitHub Desktop.
AlarmManager + NoficationManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<Button | |
android:id="@+id/button" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Button" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.elevintech.alarmmanager"> | |
<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/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<receiver android:name=".MainActivity$Receiver"/> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.elevintech.alarmmanager | |
import android.app.* | |
import android.content.BroadcastReceiver | |
import android.content.Context | |
import android.content.Intent | |
import android.graphics.Color | |
import android.os.Build | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import androidx.core.app.NotificationCompat | |
import androidx.core.app.NotificationManagerCompat | |
import androidx.core.content.ContextCompat.getSystemService | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
lateinit var alarmManager: AlarmManager | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager | |
button.setOnClickListener { | |
val intent = Intent(this, Receiver::class.java) | |
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) | |
println("button is clicked") | |
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 4000, pendingIntent) | |
} | |
} | |
class Receiver: BroadcastReceiver(){ | |
// lateinit var notificationManager: NotificationManagerCompat | |
// lateinit var notificationChannel: NotificationChannel | |
// lateinit var builder: Notification.Builder | |
var channelId = "com.elevintech.alarmmanager" | |
var description = "Notify Odometer" | |
override fun onReceive(p0: Context?, p1: Intent?) { | |
// | |
// val builder = NotificationCompat.Builder(p0!!, channelId) | |
// .setSmallIcon(R.drawable.ic_launcher_background) | |
// .setContentTitle("This is my title") | |
// .setContentText("This is my text") | |
// .setPriority(NotificationManagerCompat.IMPORTANCE_HIGH) | |
// | |
// val notificationManager = NotificationManagerCompat.from(p0) | |
// | |
// notificationManager.notify(200, builder | |
// .build()) | |
// println("message received") | |
//// | |
//// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
//// notificationChannel = NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH ) | |
//// notificationChannel.enableLights(true) | |
//// notificationChannel.lightColor = Color.BLUE | |
//// notificationChannel.enableVibration(false) | |
//// notificationManager.createNotificationChannel(notificationChannel ) | |
//// } | |
var builder = NotificationCompat.Builder(p0!!, channelId) | |
.setSmallIcon(R.drawable.ic_launcher_background) | |
.setContentTitle("some titlez") | |
.setContentText("some text") | |
.setDefaults(Notification.DEFAULT_VIBRATE) //Important for heads-up notification | |
.setPriority(Notification.PRIORITY_MAX) //Important for heads-up notification | |
val manager = p0!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
manager.notify(0, builder.build()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment