This video shows complete steps and demo to design and develop an Alarm clock App using Android Studio.
[Source Code]
package com.example.maalarm.myalarm;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextClock;
import android.widget.TimePicker;import java.util.Timer;
import java.util.TimerTask;public class MainActivity extends AppCompatActivity {
TimePicker alarmTime;
TextClock currentTime;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);alarmTime = findViewById(R.id.timePicker);
currentTime = findViewById(R.id.textClock);
final Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {if (currentTime.getText().toString().equals(AlarmTime())){
r.play();
}else{
r.stop();
}
}
}, 0, 1000);
}public String AlarmTime(){
Integer alarmHours = alarmTime.getCurrentHour();
Integer alarmMinutes = alarmTime.getCurrentMinute();
String stringAlarmMinutes;if (alarmMinutes<10){
stringAlarmMinutes = “0”;
stringAlarmMinutes = stringAlarmMinutes.concat(alarmMinutes.toString());
}else{
stringAlarmMinutes = alarmMinutes.toString();
}
String stringAlarmTime;if(alarmHours>12){
alarmHours = alarmHours – 12;
stringAlarmTime = alarmHours.toString().concat(“:”).concat(stringAlarmMinutes).concat(” PM”);
}else{
stringAlarmTime = alarmHours.toString().concat(“:”).concat(stringAlarmMinutes).concat(” AM”);
}
return stringAlarmTime;
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.maalarm.myalarm”><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/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” /><category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application></manifest>
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”com.example.maalarm.myalarm.MainActivity”><TimePicker
android:id=”@+id/timePicker”
android:layout_width=”300dp”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true” /><TextClock
android:id=”@+id/textClock”
android:layout_width=”150dp”
android:layout_height=”80dp”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”37dp” />
</RelativeLayout>
Top posts/ comments from YouTube channel
<TimePicker
android:id=”@+id/timePicker”
android:layout_width=”300dp”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true” /><TextClock
android:id=”@+id/textClock”
android:layout_width=”150dp”
android:layout_height=”80dp”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”37dp” />
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
When i run the app, it produces a security exception and requires a permission, what is the permission that i can use to play the ringtone?
No such special permission is required to access or play ringtone. As long as ringtone is correctly created using the below Ringtone manager, it should work.
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
Let me know what is the exception is being thrown?
Hello, so i ran your program on a virtual phone just like in the video and it worked fine, but when i connect as a test device my real phone the app has a problem in the result phase. It does not ring when it’s time so it’s like it doesn’t work at all. Why does this happen? As i said, in the virtual device it works perfectly. Is there a permission problem with a real phone’s ringtone? I have a Samsung Galaxy S7 Edge. Thanks a lot, the video is very helpful!
I don’t think there is any special permission needed to access ringtone through ringtone manager as shown in the tutorial. Though there might be some exceptions being thrown while running the App in your Samsung Galaxy S7. Unfortunately, I don’t have this model of phone otherwise I would have tried myself.
What I can suggest is to go to the developer options in your phone and then debug the code in Android Studio directly while your App runs on your Phone. This will be helpful in finding the exception it is throwing.
Hello again, so as i realized the problem was with the 24hour settings and the language of the device. Sadly this simple version you created works only for english-12hour settings but i think i can find a way to resolve this issue. Again thanks a lot for all the help!!!
Great ?
Thankyou sir
Sir, not working in background. Help please.
For background process try to use the below concept:
https://programmerworld.co/android/how-to-create-background-process-in-your-android-app/
Good Luck
Programmer World
–
hello sir i just wont to ask you plz in my button I don’t have date what should I do
In this code, I have shown only to choose Time. I have not handled Date to keep this code simple. However, if you want to integrate the Date selection then you can do by adding calendar option. Details of adding calendar is shown in below page:
https://programmerworld.co/android/how-to-create-a-custom-calendar-app-to-store-reminders-and-events-using-sqlite-database-in-android-studio/
Hope the above helps.
Good Luck
Programmer World
–
ausom
bruh