I am new in android. I am working on an android alarm app.I want to show an alarm between 8am to 8pm after each 30 minutes and repeat this alarm daily. My code is here. Main Activity....
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class WaterActivity extends Activity {
Button leftBTN, rightBTN;
Switch toggleSwitch;
Context context = this;
SharedPref pref;
static PendingIntent pendingIntent;
static AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_water);
leftBTN = (Button)findViewById(R.id.leftbtn);
rightBTN = (Button)findViewById(R.id.rightbtn);
toggleSwitch = (Switch)findViewById(R.id.switchbtn);
pref = new SharedPref(context);
if(pref.getValue(pref.getWaterKey()).equals("on"))
toggleSwitch.setChecked(true);
toggleSwitch.setOnCheckedChangeListener(SwitchListener);
leftBTN.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
leftBTN.setTextColor(getResources().getColor(R.color.water_color));
rightBTN.setTextColor(getResources().getColor(R.color.white_color));
leftBTN.setBackgroundResource(R.drawable.button_bg);
rightBTN.setBackgroundResource(R.drawable.button_right_bg2);
}
});
rightBTN.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
rightBTN.setTextColor(getResources().getColor(R.color.water_color));
leftBTN.setTextColor(getResources().getColor(R.color.white_color));
rightBTN.setBackgroundResource(R.drawable.button_right_bg);
leftBTN.setBackgroundResource(R.drawable.button_bg2);
}
});
}
private Switch.OnCheckedChangeListener SwitchListener = new Switch.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
pref.savePre_value(pref.getWaterKey(), "on");
Toast.makeText(context, "Reminder is on", Toast.LENGTH_SHORT).show();
Intent intentsOpen = new Intent(context, WaterReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context,111, intentsOpen, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10000, pendingIntent);
} else {
pref.savePre_value(pref.getWaterKey(), "off");
Toast.makeText(context, "Reminder is off", Toast.LENGTH_SHORT).show();
alarmManager.cancel(pendingIntent);
}
}
};
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
}
and this is my Receiver.
public class WaterReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Time is up!!!!.",
Toast.LENGTH_LONG).show();
}
}
Please help what am i missing here? How can i do this in android.any one can help me in this? Thanks in start.
Preety simple process:
It would be fairly simple to implement, sample code
public void setDailyAlarms(Context context){
// Daily Alarm
AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
Intent intent = new Intent("DAIL_ALARM_TRIGGERED");
PendingIntent pIntent = PendingIntent.getBroadcast(context,
100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);
}
public void setRepeatingAlarm(Context context){
// Alarm 30 min each'
AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("REPEATING_ALARM_TRIGGERED");
PendingIntent pIntent = PendingIntent.getBroadcast(context,
102, intent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 30*60*1000, pIntent);
}
public void cancelRepeatingAlarm(Context context){
AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("REPEATING_ALARM_TRIGGERED");
PendingIntent pIntent = PendingIntent.getBroadcast(context,
102, intent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.cancel(pIntent);
}
Now, you need a broadcast receiver to receive "DAIL_ALARM_TRIGGERED"
intent, once you receive this you call setRepeatingAlarm()
, now this will fire broadcast "REPEATING_ALARM_TRIGGERED"
once you receive this just check if present time is 8PM is yes then call cancelRepeatingAlarm()
;
I guess this would be sufficent for you to implement your logic.