Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Write a mobile application that creates alarm clock in android

1)Open eclipse or android studio and select new android project
2)Give project name and select next
3) Choose the android version.Choose the lowest android version(Android 2.2) and select next
4) Enter the package name.package name must be two word seprated by comma and click finish
5)Go to package explorer in the left hand side.select our project.
6)Go to res folder and select layout.Double click the main.xml file.Add the code below
<?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" >
   <TextView
      android:id="@+id/Title"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_margin="5px"
      android:focusable="false"
      android:focusableInTouchMode="false"
      android:gravity="center_vertical|center_horizontal"
      android:text="ALARM CLOCK"
      android:textSize="20sp"
      android:textStyle="bold" />
    <Button
        android:id="@+id/startSetDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Set Target Time"/>
    <TextView
        android:id="@+id/alarmprompt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
7) Now select mainactivity.java file and type the following code.In my coding maniactivity name is AlarmActivity.
package alarm.android;

import java.util.Calendar;
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 AlarmActivity extends Activity {
    TimePicker myTimePicker;
     Button buttonstartSetDialog;
     TextView textAlarmPrompt;
  
     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.main);
          
            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(
              AlarmActivity.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){
        //Today Set time passed, count to tomorrow
        calSet.add(Calendar.DATE, 1);
       }
     
       setAlarm(calSet);
      }};

     private void setAlarm(Calendar targetCal){

      textAlarmPrompt.setText(
        "\n\n***\n"
        + "Alarm is set@ " + 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);
   
     }
}
8)Right Click package and select new class .Give a class name as AlarmReceiver.Open AlarmReceiver.java file and type the following code.
package alarm.android;
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, "Alarm received!", Toast.LENGTH_LONG).show();

     }
}

9)Now go to main.xml and right click .select run as option and select run configuration
10) Android output is present in the android emulator as shown in below.





3 comments:

  1. Very informative post. Thanks for sharing such important information. This will be center of attraction for many



    Android Apps Development Services

    ReplyDelete
  2. This is highly informatics, crisp and clear. I think that everything has been described in systematic manner so that reader could get maximum information and learn many things. 24 hour truck tire repair

    ReplyDelete
  3. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. video alarm

    ReplyDelete

Flag Counter