0% found this document useful (0 votes)
82 views7 pages

16 Alaram Clock

The document describes the steps to create an Android alarm clock application. It involves creating a new project, adding activities for the main interface and alarm receiver, coding the main activity to set alarms and the receiver to play the alarm, and updating the manifest to register the receiver. The source code for the main activity, receiver, layout, and manifest are provided to demonstrate how the alarm is set on a toggle and played on receipt.

Uploaded by

Lovely BGM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views7 pages

16 Alaram Clock

The document describes the steps to create an Android alarm clock application. It involves creating a new project, adding activities for the main interface and alarm receiver, coding the main activity to set alarms and the receiver to play the alarm, and updating the manifest to register the receiver. The source code for the main activity, receiver, layout, and manifest are provided to demonstrate how the alarm is set on a toggle and played on receipt.

Uploaded by

Lovely BGM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Ex.

No: 13 Write a mobile application that creates alarm clock

Aim

To develop an Android Application that creates Alarm Clock.

Procedure

1. Creating a New project.


2. Open Android Studio and then click on File -> New -> New project.
3. Then type the Application name as “exno13″ and click Next.
4. Then select the Minimum SDK as shown below and click Next.
5. Then select the Empty Activity and click Next.
6. Finally click Finish.
7. It will take some time to build and load the project.
8. After completion it will look as given below.
9. Click on File -> New -> Activity -> Empty Activity.
10. Type the Activity Name as AlarmReceiver and click Finish button.
11. Thus, Second Activity For the application is created.
12. Click on app -> res -> layout -> activity_main.xml.
13. Now click on Design and your application will look as given below.
14. So now the designing part is completed.
15. Click on app -> manifests -> AndroidManifest.xml
16. Now change the activity tag to receiver tag in the AndroidManifest.xml file as shown
below
17. So now the changes are done in the Manifest
18. Click on app -> java -> com.example.exno13 -> MainActivity.
19. So now the Coding part of Main Activity is completed.
20. Click on app -> java -> com.example.exno13 -> AlarmReceiver.
21. So now the Coding part of Alarm Receiver is also completed.
22. Now run the application to see the output.

Source Code

MainActivity.java

89
package com.example.exno13;

import android.app.AlarmManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;

import android.widget.TimePicker;

import android.widget.Toast;

import android.widget.ToggleButton;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity

TimePicker alarmTimePicker;

PendingIntent pendingIntent;

AlarmManager alarmManager;

@Override

protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

90
public void OnToggleClicked(View view)

long time;

if (((ToggleButton) view).isChecked())

Toast.makeText(MainActivity.this, "ALARM ON", Toast.LENGTH_SHORT).show();

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());

calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());

Intent intent = new Intent(this, AlarmReceiver.class);

pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));

if(System.currentTimeMillis()>time)

if (calendar.AM_PM == 0)

time = time + (1000*60*60*12);

else

time = time + (1000*60*60*24);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);

else

alarmManager.cancel(pendingIntent);

Toast.makeText(MainActivity.this, "ALARM OFF", Toast.LENGTH_SHORT).show();

91
}

activity_main.xml

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

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<TimePicker

android:id="@+id/timePicker"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center" />

<ToggleButton

android:id="@+id/toggleButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:layout_margin="20dp"

android:checked="false"

android:onClick="OnToggleClicked" />

</LinearLayout>

92
AlarmReceiver.java

package com.example.exno13;

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.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver

@Override

public void onReceive(Context context, Intent intent)

Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();

Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

if (alarmUri == null)

alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);

ringtone.play();

93
AndroidManifest.xml

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

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

package="com.example.exno13">

<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.Exno13">

<activity android:name=".AlarmReceiver"></activity>

<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=".AlarmReceiver"></receiver>

</application>

</manifest>

94
Output

Conclusion

Thus Android Application that creates Alarm Clock is developed and executed successfully.

95

You might also like