In this tutorial we will see how make a alarm application in android device. Android supports alarm services. Here we are using BroadcastReceiver class to listen  for incoming information from user and then use the Toast class to display the alarm message with vibrator. Android Emulator does't make a vibrator we need to make without vibration. In mobile we have to add vibrator code. The following Try It Out demonstrates how you can do this.....

TRY IT OUT Using the AutoCompeteTextView


1.    Using Eclipse, Create an Android project and name it Alarm.

2.    Modify the main.xml file located in the res/layout folder as shown here:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/backrepeat"
android:gravity="center_horizontal">
<EditText android:layout_height="wrap_content" android:id="@+id/time"
android:layout_width="wrap_content" android:hint="Number of seconds"
android:inputType="numberDecimal"></EditText>
<Button android:text="Start Counter" android:id="@+id/ok"
android:onClick="startAlert" android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>

3.    Add the following statements to AlarmActivity.java file:

package com.alarm;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Alarm1Activity extends Activity 
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startAlert(View view) {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager)
getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_LONG).show();
}
}

3.    Add the following statements to MyBroadcastReceiver.java file:


package com.alarm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver 
{
public void onReceive(Context context, Intent intent) 
{
Toast.makeText(context, " wake up Moorthy your time is up!!!!.",
Toast.LENGTH_LONG).show();
Vibrate the mobile phone
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
}


4.    Press F11 to debug the application on the Android Emulator. The below screen shown making a alarm within in the second.



Download Code

Categories: