alarm manager ve edittext(ACİL DESTEĞE İHTİYACIM VAR):(
16.05.2016 - 08:57
editexte girdiğim verilere ek olarak alarm eklemek istiyorum bi not defteri gibi.aşağıdki kodlar ile istediğim zaman ayarı ile bildirim alabiliyorum.ama bunu hem not kayıtı yapıp hemde bu verilere bağlı olarak bildirim nasıl verdirebilirim. yardımcı olursanız sevinirim.
MainActivity
public class MainActivity extends AppCompatActivity {
DatePicker pickerDate;
TimePicker pickerTime;
Button buttonSetAlarm;
TextView info;
//EditText ed1;
final static int RQS_1 = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ed1=(EditText)findViewById(R.id.editText11);
info = (TextView)findViewById(R.id.info);
pickerDate = (DatePicker)findViewById(R.id.pickerdate);
pickerTime = (TimePicker)findViewById(R.id.pickertime);
Calendar now = Calendar.getInstance();
pickerDate.init(
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH),
null);
pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));
buttonSetAlarm = (Button)findViewById(R.id.setalarm);
buttonSetAlarm.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
Calendar current = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(pickerDate.getYear(),
pickerDate.getMonth(),
pickerDate.getDayOfMonth(),
pickerTime.getCurrentHour(),
pickerTime.getCurrentMinute(),
00);
if(cal.compareTo(current) <= 0){
//The set Date/Time already passed
Toast.makeText(getApplicationContext(),
"Invalid Date/Time",
Toast.LENGTH_LONG).show();
}else{
// String str = ed1.getText().toString();
setAlarm(cal);
}
}});
}
private void setAlarm(Calendar targetCal){
info.setText("\n\n***\n"
+ "Alarm is set@ " + targetCal.getTime() + "\n"
+ "***\n");
Intent intent = new Intent(getBaseContext(), alarm.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);
}
}
ALARM
public class alarm extends BroadcastReceiver {
private static final int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;
private final String myBlog = "HATIRLATICI";
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(context,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Exercise of Notification!")
.setContentText("merve")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.a)
.build();
notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
7
Görüntülenme
0 Beğeni