Skip to main content

Set Alarm on specified time using Broadcast reciever in Android

Alarm.java


import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class Alarm extends Activity {

    TimePicker TimePicker;
    Button Setalarm;

    TimePickerDialog timePickerDialog;

    final static int RQS_1 = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm);

        Setalarm = (Button) findViewById(R.id.Setalarm);
        Setalarm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                openTimePickerDialog(false);

            }
        });

    }

    private void openTimePickerDialog(boolean is24r) {
        Calendar calendar = Calendar.getInstance();
        timePickerDialog = new TimePickerDialog(Alarm.this, onTimeSetListener,
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), is24r);
        timePickerDialog.setTitle("Set Alarm");

        timePickerDialog.show();

    }

    OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            Calendar calNow = Calendar.getInstance();
            Calendar calSet = (Calendar) calNow.clone();

            calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calSet.set(Calendar.MINUTE, minute);
            calSet.set(Calendar.SECOND, 0);
            calSet.set(Calendar.MILLISECOND, 0);

            if (calSet.compareTo(calNow) <= 0) {

                calSet.add(Calendar.DATE, 1);
            }

            setAlarm(calSet);
        }
    };

    private void setAlarm(Calendar targetCal) {

        //
        Toast.makeText(Alarm.this, "Alarm is set at" + targetCal.getTime(),
                Toast.LENGTH_LONG).show();
        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
                pendingIntent);

    }

}




AlarmReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Toast.makeText(arg0, "Your Time is up!!!!!", Toast.LENGTH_LONG).show();

    }

}


activity_alarm.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/Setalarm"
        android:layout_width="117dp"
        android:layout_height="106dp"
        android:background="@drawable/alarm" />

    <TextView
        android:id="@+id/alarmprompt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alarm_manager"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.alarm_manager.Alarm"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="AlarmReceiver"></receiver>
    </application>

</manifest>



Screenshot 


Comments

Popular posts from this blog

Android Studio New IDE for Android Application Development

Google Launches Android Studio And New Features For Developer Console, Including Beta Releases And Staged Rollout during Google’s I/O developer conference, the company announced a group of tools for app developers, including a new developer suite called Android Studio. It’s an IDE based on  IntelliJ . This was a popular announcement, as the crowd “ooh’d” and “ahh’d” as screenshots were shown on stage. This tool has more options for Android Development, making the process faster and more productive. A “live layout” was shown that renders your app as you’re editing in realtime. Additionally, you can switch over to different layouts and screen sizes, such as 3.7 inch phone and 10-inch tablet. The team noted that this might be useful for internationalization, allowing you to quickly see what things look like without having to package up your app and install it on a device. The company says that it has “big plans” for Android Studio. DOWNLOAD ANDROID STUDIO V0...

Android Alarm Manager Example

AlarmManager Many a times we want some task to be performed at some later time in future. For Example: In SMS Scheduler we want a SMS to be send at some later time, or Task Reminder in which we want to be reminded  about a task at a particular time, to implement all these things we use AlramManager class. AlarmManager class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the  Intent  that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.                   The Alarm Manager holds a CPU wake lock as long as the ...

Android How to Send Email programmatically

In this tutorial, you will see how to send an email from your own android application. To do that, Android provides the  Intent . ACTION_SEND  , which means that your Activity want to send some data(in our case:email, cc, bcc, subject, attachements …) to another Activity(it may be an Activity in your own application, or in another application). The activity which will receive this sent data is not specified, it is up to the receiver of this action to ask the user where the data should be sent. That’s why you should give the user the choice of how to send data by wrapping it into a  Chooser   (through   createChooser ( Intent , CharSequence )  ). Send Email : 1 2 3 4 5 6 7 8 Intent intentEmail = new Intent ( Intent . ACTION_SEND ) ; intentEmail . putExtra ( Intent . EXTRA_EMAIL , new String [ ] { "your.email@gmail.com" } ) ; intentEmail . putExtra ( Intent . EXTRA_SUBJECT , "your subject" ) ; intentEmail...