Hello Friends, Today I am going to share how to use Android AlarmManager to Start Background Service Periodically. The Alarm Manager holds a CPU wake lock which guarantees that the phone will not sleep until you have finished handling the broadcast. It can also be used to perform some repetitive task outside the lifetime of your application, which means even if your app is not running, you can initiate long-running operation. such as downloading weather forecast once a day or once every fifteen minutes.
So, here is the code
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.zatackcoder.alarmmanagertest; import androidx.appcompat.app.AppCompatActivity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setAlarm(); } }); } private void setAlarm() { Intent intent = new Intent(this, MyService.class); PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent); Toast.makeText(this, "Alarm will ring after every 15 minutes interval",Toast.LENGTH_LONG).show(); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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" android:orientation="vertical" android:gravity="center" android:padding="30dp"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set Repeating Alarm"/> </LinearLayout> |
MyService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.zatackcoder.alarmmanagertest; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.provider.Settings; import android.widget.Toast; public class MyService extends Service { MediaPlayer mp; @Override public void onCreate() { super.onCreate(); mp = MediaPlayer.create(getApplicationContext(), Settings.System.DEFAULT_ALARM_ALERT_URI); mp.start(); Toast.makeText(getApplicationContext(), "Hi, I am service see you again after 15 minutes", Toast.LENGTH_LONG).show(); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } } |
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zatackcoder.alarmmanagertest"> <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"> <service android:name=".MyService" android:enabled="true" android:exported="true"></service> <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> |
Screenshot
AndroidStudio Project Download
Using Android AlarmManager to Start Background Service Periodically
1 file(s) 8.92 MB
Reference
https://developer.android.com/training/scheduling/alarms
Thanks for Stoping by
If you find this helpful then please share
Comments