- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to start Service using Alarmmanager in Kotlin Android?
This example demonstrates how to start Service using Alarmmanager in Kotlin Android.
Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="16sp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:text="Tutorials Point" android:textAlignment="center" android:textColor="@android:color/holo_blue_light" android:textSize="32sp" android:textStyle="bold" /> <Button android:id="@+id/btnStartService" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="Start Service Alarm" /> <Button android:id="@+id/btnStopService" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Cancel Service" /> </LinearLayout>
Step 3 − Add the following code to src/MainActivity.kt
import android.app.AlarmManager import android.app.PendingIntent import android.content.Context import android.content.Intent import android.os.Bundle import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import java.util.* class MainActivity : AppCompatActivity() { private lateinit var btnStart: Button private lateinit var btnStop: Button lateinit var pendingIntent: PendingIntent override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" btnStart = findViewById(R.id.btnStartService) btnStop = findViewById(R.id.btnStopService) btnStart.setOnClickListener { val myIntent = Intent(this@MainActivity, MyAlarmService::class.java) pendingIntent = PendingIntent.getService(this@MainActivity, 0, myIntent, 0) val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager val calendar: Calendar = Calendar.getInstance() calendar.timeInMillis = System.currentTimeMillis() calendar.add(Calendar.SECOND, 3) alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent) Toast.makeText(baseContext, "Starting Service Alarm", Toast.LENGTH_LONG).show() } btnStop.setOnClickListener { val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager alarmManager.cancel(pendingIntent) Toast.makeText(baseContext, "Service Cancelled", Toast.LENGTH_LONG).show() } } }
Step 4 − Add the following code to src/MyAlarmService.kt
import android.app.Service import android.content.Intent import android.os.IBinder import android.widget.Toast @Suppress("DEPRECATION") class MyAlarmService : Service() { override fun onCreate() { Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show(); super.onCreate() } override fun onBind(intent: Intent?): IBinder? { Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show(); return null } override fun onDestroy() { super.onDestroy() Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show() } override fun onStart(intent: Intent?, startId: Int) { super.onStart(intent, startId) Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show() } override fun onUnbind(intent: Intent?): Boolean { Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show() return super.onUnbind(intent) } }
Step 5 − Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.kotlipapp"> <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> </application> </manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
Click here to download the project code.