Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Set Alarm Using TimePicker in Android

    • 0
    • 0
    • 0
    • 0
    • 3
    • 0
    • 0
    • 0
    • 7.67k
    Comment on it

    This tutorial will help you in creating an android application to set alarm using TimePicker. This is a basic android tutorial which will make you familiar with Intents , Pending Intents and BroadcastReceiver.

    Step 1: Create an android Project Name TimeActivity

    Step 2: Create main the layout file activity_main.xml

    Step 3:Set Button,TimePicker and TextView

    Step 4:Then SetAlarm by creating the pending intent

    Step 5:Create the BroadcastReceiver to recive and set the Ringtone

    step 6: Set user permissions in manifest file

    First create the layout file set buttons and textview..

    then create the mainactivity .

    Sample code::

     package com.example.timeactivity;
    
    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;
    
    public class MainActivity extends Activity {
    
     TimePicker myTimePicker;
     Button buttonstartSetDialog;
     TextView textAlarmPrompt;
    
     TimePickerDialog timePickerDialog;
    
     final static int RQS_1 = 1;
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            startActivity(new Intent(MainActivity.this,Ringtones.class));
    
            textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);
    
            buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
            buttonstartSetDialog.setOnClickListener(new OnClickListener(){
    
       @Override
       public void onClick(View v) {
        textAlarmPrompt.setText("");
        openTimePickerDialog(false);
    
       }});
    
        }
    
    
     private void openTimePickerDialog(boolean is24r){
      Calendar calendar = Calendar.getInstance();
    
      timePickerDialog = new TimePickerDialog(
        MainActivity.this, 
        onTimeSetListener, 
        calendar.get(Calendar.HOUR_OF_DAY), 
        calendar.get(Calendar.MINUTE), 
        is24r);
      timePickerDialog.setTitle("Set Alarm Time");  
    
      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){
    
      textAlarmPrompt.setText(
        "\n\n\n"
        + "AlarmSet  " + targetCal.getTime() + "\n"
        + "\n");
    
      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);
    
     }
    
    }
    

    Then create the AlarmReceiver extending BroadcastReceiver.

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.media.Ringtone;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.os.Vibrator;
    import android.widget.Toast;
    
    public class AlarmReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent arg1) {
            Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
    
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            Ringtone r = RingtoneManager.getRingtone(context, notification);
            r.play();
    
            Vibrator vibrator = (Vibrator)context 
                    .getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(5000);
        }
    
    }
      

 3 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: