package com.example.deep.myalarm;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
TextView t1, t2;
ImageView im1, im2;
int ho, mi, da, mo, ye;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = findViewById(R.id.time1);
t2 = findViewById(R.id.date1);
im1 = findViewById(R.id.time2);
im2 = findViewById(R.id.date2);
final Calendar c = Calendar.getInstance();
im1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TimePickerDialog td = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int h, int m) {
ho = h;
mi = m;
t1.setText(h + "-" + m);
}
}, c.get(Calendar.HOUR), c.get(Calendar.MINUTE), false);
td.show();
}
});
im2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePickerDialog dp = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int y, int m, int d) {
ye = y;
mo = m;
da = d;
t2.setText(y + "-" + (m + 1) + "-" + d);
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
dp.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
dp.show();
}
});
}
public void setAlarm(View v) {
if (t1.getText().toString().equals("") && t2.getText().toString().equals("")) {
Toast.makeText(this, "Please Set Date And Time", Toast.LENGTH_SHORT).show();
} else {
setMyAlarm();
}
}
private void setMyAlarm() {
Calendar cc = Calendar.getInstance();
cc.set(ye, mo, da, ho, mi, 0);
Intent in = new Intent(this, MyReceiver.class);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, in, 0);
am.set(AlarmManager.RTC, cc.getTimeInMillis(), pendingIntent);
Toast.makeText(this, "Alarm Set Successfully", Toast.LENGTH_SHORT).show();
}
}
________________________________________________________________________
package com.example.deep.myalarm;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm Is Popup", Toast.LENGTH_SHORT).show();
setNotification(context);
}
private void setNotification(Context context) {
Intent in = new Intent(context, MainActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), in, 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("My Alarm Title")
.setContentText("Task Is Complete")
.setContentIntent(pIntent).setSmallIcon(R.drawable.time).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// hide the notification after its selected noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}
_____________________________________________________________________
< ?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">
< LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
< TextView
android:id="@+id/time1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="20dp" />
< ImageView
android:id="@+id/time2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/time" />
< /LinearLayout>
< LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
< TextView
android:id="@+id/date1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="20dp" />
< ImageView
android:id="@+id/date2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/date" />
< /LinearLayout>
< Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Set Alarm"
android:onClick="setAlarm"/>
< /LinearLayout>
1 comment:
How to repeat alarms on different days as per users choice.
Post a Comment