Skip to content

Instantly share code, notes, and snippets.

View Lavanyagaur22's full-sized avatar
🚩

Lavanya gaur Lavanyagaur22

🚩
View GitHub Profile
private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.channel_name) //name of the channel
val descriptionText = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_DEFAULT //importance of the channel
// Create a NotificationChannel
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
//Create a simple notification, first - > Channel ID
val simpleNotification = NotificationCompat.Builder(this, "first")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Hello There")
.setContentText("General Master!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT).build()
//Show the notification, nm is the NotificationManager object
nm.notify(id, simpleNotification)
val bigTextStyle = NotificationCompat.Builder(this, "first")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Hello There")
.setContentText("General Master!")
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(getString(R.string.big_string))
)
.setPriority(NotificationCompat.PRIORITY_DEFAULT).build()
val icon = BitmapFactory.decodeResource(
this.resources,
R.drawable.ic_launcher_foreground
)
val not = NotificationCompat.Builder(this, "first")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Hello There")
.setContentText("General Kenobi!")
.setLargeIcon(icon)
val i = Intent()
i.action = Intent.ACTION_VIEW
i.data = Uri.parse("https://www.google.com")
val pi: PendingIntent =
PendingIntent.getActivity(this, 12345, i, PendingIntent.FLAG_UPDATE_CURRENT)
val actionedNotification = NotificationCompat.Builder(this, "first")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Hello There")
// Create an intent
val i = Intent()
i.action = Intent.ACTION_VIEW
i.data = Uri.parse("https://www.google.com")
// Craete a pending intent
val pi: PendingIntent =
PendingIntent.getActivity(this, 12345, i, PendingIntent.FLAG_UPDATE_CURRENT)
val actionedNotification = NotificationCompat.Builder(this, "first")
.setSmallIcon(R.drawable.ic_launcher_foreground)
private lateinit var sensorManager: SensorManager
...
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
//To get a list of all sensors present on the device.
val deviceSensors: List<Sensor> = sensorManager.getSensorList(Sensor.TYPE_ALL)
//To get the default gravity sensor present on the device
val gravSensor = sensorManager.getDefaultSensor(TYPE_GRAVITY)
//To check the power of the sensor if present
val power = gravSensor.getPower()
package com.lavanya.androidsensors
import android.content.Context
import android.graphics.Color
import android.hardware.Sensor
import android.hardware.Sensor.TYPE_GRAVITY
import android.hardware.Sensor.TYPE_PROXIMITY
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
val myObject = MyObject()
//set variables of 'myObject', etc..
// val editor = prefs.edit() -> gives the SharedPreferences.Editor
val gson = Gson();
val json = gson.toJson(myObject);
editor.putString("MyObject", json);
editor.apply();