Tuesday, March 16, 2021

Android AlarmManager

 

Android AlarmManager

Android AlarmManager allows you to access system alarm.

By the help of Android AlarmManager in android, you can schedule your application to run at a specific time in the future. It works whether your phone is running or not.

The Android AlarmManager holds a CPU wake lock that provides guarantee not to sleep the phone until broadcast is handled.

Android AlarmManager Example

Let's see a simple AlarmManager example that runs after a specific time provided by user.

activity_main.xml

You need to drag only a edittext and a button as given below.

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.alarmmanager.MainActivity">  
  8.   
  9.     <Button  
  10.         android:id="@+id/button"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="Start"  
  14.         android:layout_alignParentBottom="true"  
  15.         android:layout_centerHorizontal="true"  
  16.         android:layout_marginBottom="103dp" />  
  17.   
  18.     <EditText  
  19.         android:id="@+id/time"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignParentTop="true"  
  23.         android:layout_centerHorizontal="true"  
  24.         android:layout_marginTop="22dp"  
  25.         android:ems="10" />  
  26. </RelativeLayout>  


Activity class

The activity class starts the alarm service when user clicks on the button.

File: MainActivity.java
  1. package example.javatpoint.com.alarmmanager;  
  2.   
  3. import android.app.AlarmManager;  
  4. import android.app.PendingIntent;  
  5. import android.content.Intent;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends AppCompatActivity {  
  14.     Button start;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         start= findViewById(R.id.button);  
  20.   
  21.         start.setOnClickListener(new View.OnClickListener() {  
  22.             @Override  
  23.             public void onClick(View view) {  
  24.                 startAlert();  
  25.             }  
  26.         });  
  27.     }  
  28.   
  29.     public void startAlert(){  
  30.         EditText text = findViewById(R.id.time);  
  31.         int i = Integer.parseInt(text.getText().toString());  
  32.         Intent intent = new Intent(this, MyBroadcastReceiver.class);  
  33.         PendingIntent pendingIntent = PendingIntent.getBroadcast(  
  34.                 this.getApplicationContext(), 234324243, intent, 0);  
  35.         AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
  36.         alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()  
  37.                 + (i * 1000), pendingIntent);  
  38.         Toast.makeText(this"Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();  
  39.     }  
  40. }  

Let's create BroadcastReceiver class that starts alarm.

File: MyBroadcastReceiver.java
  1. package example.javatpoint.com.alarmmanager;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.media.MediaPlayer;  
  7. import android.widget.Toast;  
  8.   
  9. public class MyBroadcastReceiver extends BroadcastReceiver {  
  10.     MediaPlayer mp;  
  11.     @Override  
  12.     public void onReceive(Context context, Intent intent) {  
  13.         mp=MediaPlayer.create(context, R.raw.alarm);  
  14.         mp.start();  
  15.         Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();  
  16.     }  
  17. }  

File: AndroidManifest.xml

You need to provide a receiver entry in AndroidManifest.xml file.

  1. <receiver android:name="MyBroadcastReceiver" >  
  2. </receiver>  

Let's see the full code of AndroidManifest.xml file.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="example.javatpoint.com.alarmmanager">  
  4.   
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@mipmap/ic_launcher"  
  8.         android:label="@string/app_name"  
  9.         android:roundIcon="@mipmap/ic_launcher_round"  
  10.         android:supportsRtl="true"  
  11.         android:theme="@style/AppTheme">  
  12.         <activity android:name=".MainActivity">  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.MAIN" />  
  15.   
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.             </intent-filter>  
  18.         </activity>  
  19.         <receiver android:name="MyBroadcastReceiver" >  
  20.         </receiver>  
  21.     </application>  
  22.   
  23. </manifest>  

Output:

android alarmmanager example output 1 android alarmmanager example output 1 android alarmmanager example output 1

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile