I am writing an android application which is an alarm clock
I am writing an android application which is an alarm clock, and I need assistance on this part.
Add a new, one-time or recursive (allow users to select) alarm for a specific date and time. The
alarm should display the user defined message in the notification bar and the location where it
was set. The user input should be
a. The date and time that the alarm is set to.
b. The time zone. By default it should be the current time zone of the device but the user
should be able to change it.
c. User defined message
d. User Location.
Solution
Open Android Studio and then click on File -> New -> New project.
2)Then type the Application name as “alm and click Next.
3)Then select the Minimum SDK as shown below and click Next.
4)Then select the Empty Activity and click Next
5)Finally click Finish
for android Activity
6)Click on File -> New -> Activity -> Empty Activity
7)Type the Activity Name as AlarmReceiver and click Finish button.
for the second activity
Click on app -> res -> layout -> activity_main.xml.
code for first or main activity will be
<?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>
Now click on Design
8)Changes for manifest
app -> manifests -> AndroidManifest.xml
Now change the activity tag to receiver tag in the AndroidManifest.xml file
code for manifest.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"
package=\"com.example.exno11\" >
<application
android:allowBackup=\"true\"
android:icon=\"@mipmap/ic_launcher\"
android:label=\"@string/app_name\"
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>
<receiver android:name=\".AlarmReceiver\" >
</receiver>
</application>
</manifest>
package com.example.exno11;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.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);
}
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();
}
}
}
coding for receiving alarm
AlarmReceiver.java:
?
package com.example.alm;
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();
}
}
here we can use
generally these classes are used to implement certain default function for developing an alarm clock .It can be used with multiple ways like
You can use TimeZone.setDefault() which will change the TimeZone for the current process only. But as noted in the docs, this is not garanteed to last for the whole application lifecycle.
You can use setTimeZone() of AlarmManager to change the TimeZone of the whole device. But you need the \"SET_TIME_ZONE\"-permission for that.
public class AlarmManager
extends Object
setTimeZone()- Sets the system’s default time zone or you can use
For Setting of the Time Zone through programming you need to use the Date Class.
You need to use the setTimeZone() method of SimpleDateFormat Class
| package com.example.alm; 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(); } } |





