Membuat Alarm Sahur
Membuat Alarm Sahur
Membuat Alarm Sahur
<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>
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textAlarmPrompt = (TextView) findViewById(R.id.alarmprompt);
@Override
public void onClick(View v) {
textAlarmPrompt.setText("");
openTimePickerDialog(false);
}
});
timePickerDialog.show();
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
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) {
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;
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();
}
<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" />
<receiver android:name=".AlarmReceiver"
android:process=":remote"/>
</application>
</manifest>
--------------------------------------------------------
<receiver android:name=".AlarmReceiver"
android:process=":remote"/>
--------------------------------------------------------------------
Sebelum </application>