Monday, 14 February 2022

Alarm Manager Example in Android

 



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <Button

        android:id="@+id/btn_start_alarm"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:text="Start Alarm"

        android:textAllCaps="false"/>


</RelativeLayout>



MainActivity.java

import androidx.appcompat.app.AppCompatActivity;


import android.app.AlarmManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;


public class MainActivity extends AppCompatActivity {


    private Button mBtnStart;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        mBtnStart = findViewById(R.id.btn_start_alarm);

        mBtnStart.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                startAlarm(MainActivity.this);

            }

        });

    }


    public static void startAlarm(Context context) {

        Intent intent = new Intent(context, MyAlarmReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(

                context.getApplicationContext(), 0 , intent, 0);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()

                + (10 * 1000), pendingIntent); //10 seconds interval

    }

}



MyAlarmReceiver.java

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;


public class MyAlarmReceiver extends BroadcastReceiver {

    @Override

    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Alarm Started Successfully", Toast.LENGTH_SHORT).show();

    }

}  



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.surabhiglobal.alarmexample">


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


    <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/Theme.AlarmExample">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


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

            </intent-filter>

        </activity>


        <receiver android:name=".MyAlarmReceiver" />


    </application>


</manifest>

No comments:

Post a Comment

Alarm Manager Example in Android