Membuat Alarm Sahur

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

MEMBUAT ALARM SAHUR

Ikuti langkah – langkah sebagai berikut :

1. Buka Android studio nya


2. Buat Project Baru dengan Nama Alarm_Sahur
3. Buatlah folder di res dengan nama row, kemudian copy kan file sahur.mp3 kedalam folder raw
yang tadi baru di buat.
4. Sesuaikan tampilan xml dari file activity_main.xml menjadi

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Alarm Sahur ...!!" />
<Button
android:id="@+id/startSetDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Target Time"/>
<TextView
android:id="@+id/alarmprompt"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

5. Sesuaikan tampilan xml dari file MainActivity.Java menjadi

package dadang.id.alarm_sahur;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends Activity {

TimePicker myTimePicker;
Button buttonstartSetDialog;
TextView textAlarmPrompt;

TimePickerDialog timePickerDialog;

final static int RQS_1 = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textAlarmPrompt = (TextView) findViewById(R.id.alarmprompt);

buttonstartSetDialog = (Button) findViewById(R.id.startSetDialog);


buttonstartSetDialog.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
textAlarmPrompt.setText("");
openTimePickerDialog(false);

}
});

private void openTimePickerDialog(boolean is24r) {


Calendar calendar = Calendar.getInstance();

timePickerDialog = new TimePickerDialog(MainActivity.this,


onTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), true);
timePickerDialog.setTitle("Set Alarm Time");

timePickerDialog.show();

TimePickerDialog.OnTimeSetListener onTimeSetListener = new


TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

Calendar calNow = Calendar.getInstance();


Calendar calSet = (Calendar) calNow.clone();

calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);

if (calSet.compareTo(calNow) <= 0) {
// Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}

setAlarm(calSet);
}
};
private void setAlarm(Calendar targetCal) {

textAlarmPrompt.setText("\n\n***\n" + "Alarm is set@ "


+ targetCal.getTime() + "\n" + "***\n");

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


PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
pendingIntent);

6. Buat file java baru dengan nama AlarmReceiver, dan Sesuaikan tampilan code java nya menjadi

package dadang.id.alarm_sahur;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

MediaPlayer player;

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Alarm aktif!", Toast.LENGTH_LONG).show();
player = MediaPlayer.create(context, R.raw.sahur);
player.start();
}

7. Sesuaikan manifest.xml nya menjadi

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dadang.id.alarm_sahur">

<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>

<receiver android:name=".AlarmReceiver"
android:process=":remote"/>
</application>

</manifest>

Perhatikan code diatas hanya menambahkan :

--------------------------------------------------------
<receiver android:name=".AlarmReceiver"
android:process=":remote"/>

--------------------------------------------------------------------

Sebelum </application>

You might also like