Layout Design
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
" rel="nofollow">http://schemas.android.com/apk/res/android"
</a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
" rel="nofollow">http://schemas.android.com/apk/res-auto"
</a> xmlns:tools="<a href="http://schemas.android.com/tools"
" rel="nofollow">http://schemas.android.com/tools"
</a> android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.razormist.simplealarmapplication.MainActivity">
<TextView
android:id="@+id/tv_display"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Alarm not set"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>
<TimePicker
android:layout_below="@+id/tv_display"
android:id="@+id/tp_time"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<Button
android:id="@+id/btn_set"
android:layout_below="@+id/tp_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Set Alarm"/>
<Button
android:id="@+id/btn_reset"
android:layout_below="@+id/btn_set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reset Alarm"/>
</RelativeLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"
" rel="nofollow">http://schemas.android.com/apk/res/android"
</a> package="com.razormist.simplealarmapplication">
<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"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmAdapter"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
Alarm Adapter
package com.razormist.simplealarmapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.provider.Settings;
/**
* Created by Arvin on 5/5/2018.
*/
public class AlarmAdapter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
mediaPlayer.start();
}
}
Main Activity
TimePicker tp_time;
TextView tv_display;
Button btn_set, btn_reset;
onCreate Method
tv_display = (TextView)findViewById(R.id.tv_display);
tp_time = (TimePicker)findViewById(R.id.tp_time);
btn_set = (Button)findViewById(R.id.btn_set);
btn_reset = (Button)findViewById(R.id.btn_reset);
btn_set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
if(Build.VERSION.SDK_INT >= 23) {
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
tp_time.getHour(),
tp_time.getMinute(),
0
);
}else{
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
tp_time.getCurrentHour(),
tp_time.getCurrentMinute(),
0
);
}
setAlarm(calendar.getTimeInMillis(), calendar);
}
private void setAlarm(long timeInMillis, Calendar c) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmAdapter.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC, timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(MainActivity.this, "Alarm Set", Toast.LENGTH_SHORT).show();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int ampm = c.get(Calendar.AM_PM);
String day = "";
if(ampm == Calendar.AM){
day = "AM";
}else if(ampm == Calendar.PM){
day = "PM";
}
String timeText = "Alarm set for: ";
timeText += hour +": " + minute + " " + day;
tv_display.setText(timeText);
}
});
btn_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmAdapter.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmManager.cancel(pendingIntent);
tv_display.setText("Alarm not set");
}
});
Source Code Android Aplikasi Alarm
Reviewed by kilhem
on
5/08/2018 01:56:00 pm
Rating:
No comments:
Jangan malu untuk berkomentar