0% found this document useful (0 votes)
75 views8 pages

Android Alarm Clock Setup Guide

Mobile App Lab Alarm Clock Experiment
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views8 pages

Android Alarm Clock Setup Guide

Mobile App Lab Alarm Clock Experiment
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Alarm Clock

Step 1: Open Android Studio, Click on New Project and Choose


Empty Activity.

Step 2: Pre-requisites:

Gradle (module)

buildFeatures{
viewBinding true
}

[Link]

<color name="lavender">#8692f7</color>

[Link]

<resources xmlns:tools="[Link]
<!-- Base application theme. -->
<style name="[Link]" parent="[Link]">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/lavender</item>
<item name="colorPrimaryVariant">@color/lavender</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
Drawables

Add vector assets – notifications icon.

[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]"
tools:targetApi="31">
<activity
android:name=".NotificationActivity"
android:exported="false">
<meta-data
android:name="[Link].lib_name"
android:value="" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
<meta-data
android:name="[Link].lib_name"
android:value="" />
</activity>
<receiver android:name=".AlarmReceiver"/>
</application>
</manifest>

Step 3: XML Layouts:

activity_main.xml

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


<LinearLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:background="@drawable/alarm"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select \nTime"
android:id="@+id/selectTime"
android:textSize="40sp"
android:layout_marginTop="280dp"
android:layout_gravity="center_horizontal"
android:textColor="@color/white"
android:textAlignment="center"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/setAlarm"
android:text="Set \nAlarm"
android:textSize="20sp"
android:padding="8dp"
android:layout_marginStart="40dp"
android:layout_marginTop="250dp"/>
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/cancelAlarm"
android:text="Cancel \nAlarm"
android:textSize="20sp"
android:padding="8dp"
android:layout_alignParentEnd="true"
android:layout_marginTop="250dp"
android:layout_marginEnd="40dp"/>
</RelativeLayout>
</LinearLayout>

activity_notifications.xml

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


<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NotificationActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to \nNotification Activity"
android:textSize="36sp"
android:textAlignment="center"
android:textColor="@color/lavender"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</[Link]>

Step 4: Java Files

[Link]

package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent nextActivity = new Intent(context, [Link]);
[Link](Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = [Link](context, 0, nextActivity, 0);
[Link] builder = new [Link](context, "androidknowledge")
.setSmallIcon([Link].ic_baseline_notifications_24)
.setContentTitle("Reminder")
.setContentText("It's time to wake up")
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent);
NotificationManagerCompat notificationManagerCompat = [Link](context);
[Link](123,[Link]());
}
}

[Link]
package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

private ActivityMainBinding binding;


private MaterialTimePicker timePicker;
private Calendar calendar;
private AlarmManager alarmManager;
private PendingIntent pendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
binding =
[Link](getLayoutInflater());
setContentView([Link]());

createNotificationChannel();

[Link](new
[Link]() {
@Override
public void onClick(View view) {
timePicker = new [Link]()
.setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(12)
.setMinute(0)
.setTitleText("Select Alarm Time")
.build();

[Link](getSupportFragmentManager(),
"androidknowledge");
[Link](new
[Link]() {
@Override
public void onClick(View view) {
if ([Link]() > 12){
[Link](
[Link]("%02d",
([Link]()-12)) +":"+ [Link]("%02d",
[Link]())+"PM"
);
} else {

[Link]([Link]()+":" +
[Link]()+ "AM");
}

calendar = [Link]();
[Link](Calendar.HOUR_OF_DAY,
[Link]());
[Link]([Link],
[Link]());
[Link]([Link], 0);
[Link]([Link], 0);
}
});
}
});

[Link](new
[Link]() {
@Override
public void onClick(View view) {
alarmManager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent([Link],
[Link]);
pendingIntent =
[Link]([Link], 0, intent, 0);

[Link](AlarmManager.RTC_WAKEUP,
[Link](), AlarmManager.INTERVAL_DAY,
pendingIntent);
[Link]([Link], "Alarm Set",
Toast.LENGTH_SHORT).show();
}
});

[Link](new
[Link]() {
@Override
public void onClick(View view) {
Intent intent = new Intent([Link],
[Link]);
pendingIntent =
[Link]([Link], 0, intent, 0);

if (alarmManager == null){
alarmManager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
}
[Link](pendingIntent);
[Link]([Link], "Alarm
Canceled", Toast.LENGTH_SHORT).show();
}
});

private void createNotificationChannel(){


if ([Link].SDK_INT >= Build.VERSION_CODES.O){
CharSequence name = "akchannel";
String desc = "Channel for Alarm Manager";
int imp = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new
NotificationChannel("androidknowledge", name, imp);
[Link](desc);

NotificationManager notificationManager =
getSystemService([Link]);

[Link](channel);
}
}
}
[Link]

You might also like