0% found this document useful (0 votes)
6 views8 pages

Name: Adarsh Shivam Reg No.: 12115538 Roll No.: A22 Sec:on: KO305 Ca: Ca2

The document describes how to create an Android application to demonstrate notifications scheduling using job scheduler. It includes code for the main activity layout and class, a notification job service class, and manifest configuration to schedule and receive notifications at specified times.

Uploaded by

Meena Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Name: Adarsh Shivam Reg No.: 12115538 Roll No.: A22 Sec:on: KO305 Ca: Ca2

The document describes how to create an Android application to demonstrate notifications scheduling using job scheduler. It includes code for the main activity layout and class, a notification job service class, and manifest configuration to schedule and receive notifications at specified times.

Uploaded by

Meena Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Name : Adarsh Shivam

Reg No. : 12115538


Roll No. : A22
Sec:on: KO305
CA: CA2

Q) Create an android applica0on to demonstrate no0fica0ons


scheduling using job scheduler.
Solu%on:

ac0vity_main.xml:
<?xml version="1.0" encoding="u7-8"?>
<Rela=veLayout xmlns:android="hCp://schemas.android.com/apk/res/android"
xmlns:tools="hCp://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="@android:color/darker_gray"
tools:context=".MainAc=vity">

<BuCon
android:id="@+id/btnScheduleNo=fica=on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start No=fica=on"
android:layout_centerInParent="true"
android:backgroundTint="@android:color/holo_green_dark"/>

<BuCon
android:id="@+id/btnCancelNo=fica=on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel No=fica=on"
android:layout_below="@id/btnScheduleNo=fica=on"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"
android:backgroundTint="@android:color/holo_red_dark"/>

</Rela=veLayout>

MainAc0vity.kt:
package com.example.ca2
import android.app.No=fica=onChannel
import android.app.No=fica=onManager
import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.widget.BuCon
import android.widget.Toast
import androidx.appcompat.app.AppCompatAc=vity

class MainAc=vity : AppCompatAc=vity() {

private val CHANNEL_ID = "no=fica=on_channel"


private val JOB_ID = 123

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.ac#vity_main)

window.decorView.setBackgroundColor(resources.getColor(android.R.color.whit
e))

createNo=fica=onChannel()

val btnScheduleNo=fica=on =
findViewById<BuCon>(R.id.btnScheduleNo#fica#on)
btnScheduleNo=fica=on.setOnClickListener {
scheduleNo=fica=on()
}

val btnCancelNo=fica=on =
findViewById<BuCon>(R.id.btnCancelNo#fica#on)
btnCancelNo=fica=on.setOnClickListener {
cancelNo=fica=on()
}
}

private fun createNo=fica=onChannel() {


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "No=fica=on Channel"
val descrip=onText = "Channel for scheduled no=fica=ons"
val importance = No=fica=onManager.IMPORTANCE_DEFAULT
val channel = No=fica=onChannel(CHANNEL_ID, name, importance).apply
{
descrip#on = descrip=onText
}

val no=fica=onManager =
getSystemService(Context.NOTIFICATION_SERVICE) as
No=fica=onManager
no=fica=onManager.createNo=fica=onChannel(channel)
}
}

private fun scheduleNo=fica=on() {


val componentName = ComponentName(this,
No=fica=onJobService::class.java)
val jobInfo = JobInfo.Builder(JOB_ID, componentName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true)
.build()

val jobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as


JobScheduler
val resultCode = jobScheduler.schedule(jobInfo)
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Toast.makeText(this, "No=fica=on scheduled successfully",
Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Failed to schedule no=fica=on",
Toast.LENGTH_SHORT).show()
}
}

private fun cancelNo=fica=on() {


val jobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as
JobScheduler
jobScheduler.cancel(JOB_ID)
Toast.makeText(this, "No=fica=on cancelled", Toast.LENGTH_SHORT).show()
}
}

No0fica0onJobService.kt:
package com.example.ca2
import android.app.No=fica=on
import android.app.No=fica=onManager
import android.app.PendingIntent
import android.app.job.JobParameters
import android.app.job.JobService
import android.content.Context
import android.content.Intent
import androidx.core.app.No=fica=onCompat

class No=fica=onJobService : JobService() {

private val CHANNEL_ID = "no=fica=on_channel"


private val NOTIFICATION_ID = 101

override fun onStartJob(params: JobParameters?): Boolean {


showNo=fica=on()
return false
}
override fun onStopJob(params: JobParameters?): Boolean {
return false
}

private fun showNo=fica=on() {


val intent = Intent(this, MainAc=vity::class.java)
val pendingIntent = PendingIntent.getAc=vity(this, 0, intent, 0)

val no=fica=on = No=fica=onCompat.Builder(this, CHANNEL_ID)


.setContentTitle("Scheduled No=fica=on")
.setContentText("This is a scheduled no=fica=on")
.setSmallIcon(R.drawable.ic_no#fica#on_custom) // <-- Add icon here
.setContentIntent(pendingIntent)
.setPriority(No=fica=onCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
.build()

val no=fica=onManager =
getSystemService(Context.NOTIFICATION_SERVICE) as
No=fica=onManager
no=fica=onManager.no=fy(NOTIFICATION_ID, no=fica=on)
}

colors.xml:
<?xml version="1.0" encoding="u7-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="green">#4CAF50</color>
<color name="red">#F44336</color>
</resources>

styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkAc=onBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

AndroidManifest.xml:
<?xml version="1.0" encoding="u7-8"?>
<manifest xmlns:android="hCp://schemas.android.com/apk/res/android"
package="com.example.ca2">

<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<applica=on
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">

<ac=vity
android:name=".MainAc=vity"
android:exported="true">
<intent-filter>
<ac=on android:name="android.intent.ac=on.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</ac=vity>

<service
android:name=".No=fica=onJobService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" />
</applica=on>

</manifest>

OUTPUT:
Thank YOU…..:)

You might also like