Past notifications showing with current notifications - java

I am using the AlarmManager for repeating alarms. I've set repeating alarms for Mondays, Thursdays and Saturdays. The alarm shows correctly on Monday but when Thursday comes, it displays both notifications for Monday and Thursday together, and shows all three notifications on the Saturdays. How do I code it such that when the first alarm is done, it doesn't show on subsequent alarms in that same week, instead it goes to the next weeks?
EDIT1 - Code
MainActivity
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_settings:
EnableNoti();
return true;
}
return super.onOptionsItemSelected(item);
}
private void EnableNoti() {
Calendar calendar = Calendar.getInstance();
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
Calendar calendar3 = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, 1); // Sunday
calendar1.set(Calendar.DAY_OF_WEEK, 4); // Wednesday
calendar2.set(Calendar.DAY_OF_WEEK, 6); // Friday
calendar3.set(Calendar.DAY_OF_WEEK, 5); // Thursday
calendar3.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); // First Thursday of
// Each Month
Date now = new Date(System.currentTimeMillis());
if (calendar3.getTime().before(now)) {
calendar3.add(Calendar.MONTH, 1);
} else if (calendar.getTime().before(now)) {
calendar.add(Calendar.HOUR, 168);
} else if (calendar1.getTime().before(now)) {
calendar1.add(Calendar.HOUR, 168);
} else if (calendar2.getTime().before(now)) {
calendar2.add(Calendar.HOUR, 168);
}
// Sunday
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
// Wednesday
calendar1.set(Calendar.HOUR_OF_DAY, 17);
calendar1.set(Calendar.MINUTE, 30);
calendar1.set(Calendar.SECOND, 0);
calendar1.set(Calendar.AM_PM, Calendar.PM);
// Friday
calendar2.set(Calendar.HOUR_OF_DAY, 17);
calendar2.set(Calendar.MINUTE, 30);
calendar2.set(Calendar.SECOND, 0);
calendar2.set(Calendar.AM_PM, Calendar.PM);
// Thursday
calendar3.set(Calendar.HOUR_OF_DAY, 22);
calendar3.set(Calendar.MINUTE, 0);
calendar3.set(Calendar.SECOND, 0);
calendar3.set(Calendar.AM_PM, Calendar.PM);
Intent myIntent = new Intent(TheBeginningEnglish.this,
MyReceiverEnglish.class);
pendingIntent = PendingIntent.getBroadcast(TheBeginningEnglish.this, 0,
myIntent, 0);
Intent myIntent1 = new Intent(TheBeginningEnglish.this,
MyReceiver1English.class);
pendingIntent1 = PendingIntent.getBroadcast(TheBeginningEnglish.this,
0, myIntent1, 0);
Intent myIntent2 = new Intent(TheBeginningEnglish.this,
MyReceiver2English.class);
pendingIntent2 = PendingIntent.getBroadcast(TheBeginningEnglish.this,
0, myIntent2, 0);
Intent myIntent3 = new Intent(TheBeginningEnglish.this,
MyReceiver3English.class);
pendingIntent3 = PendingIntent.getBroadcast(TheBeginningEnglish.this,
0, myIntent3, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
pendingIntent); // every Sunday
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar1.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
pendingIntent1); // every Wednesday
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar2.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
pendingIntent2); // every Friday
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar3.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 31,
pendingIntent3); // every first Thursday
}
Receiver.java
public class MyReceiverEnglish extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Intent service = new Intent(context, MyAlarmServiceEnglish.class);
context.startService(service);
}
}
AlarmService.java
public class MyAlarmServiceEnglish extends Service {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
private NotificationManager mManager;
Notification notification;
// Notification notification;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
#Override
public void onStart(Intent intent, int startId) {
onStartCommand(intent, 0, startId);
}
#SuppressWarnings({ "static-access", "deprecation" })
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
mManager = (NotificationManager) this.getApplicationContext()
.getSystemService(
this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),
TheBeginning1English.class);
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
Notification notification = new Notification(
R.drawable.ic_launcher,
"Its Sunday!.",
System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent1 = PendingIntent
.getActivity(this.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(),
"Testing", "Its Sunday.",
pendingNotificationIntent1);
mManager.notify(0, notification);
} else {
PendingIntent pendingNotificationIntent1 = PendingIntent
.getActivity(this.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Test Test App")
.setContentText("Its Sunday!.")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingNotificationIntent1)
.setSmallIcon(R.drawable.ic_launcher).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mManager.notify(0, notification);
}
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
Thats it,in the main activity, you will see four separate calendar variables, I had to create a Receiver,Receiver1,Receiver2,Receiver3, and AlarmService1,2,3. for each calendar, not the best way, but only thing I could figure. I added the:
Date now = new Date(System.currentTimeMillis());
if (calendar3.getTime().before(now)) {
calendar3.add(Calendar.MONTH, 1);
} else if (calendar.getTime().before(now)) {
calendar.add(Calendar.HOUR, 168);
} else if (calendar1.getTime().before(now)) {
calendar1.add(Calendar.HOUR, 168);
} else if (calendar2.getTime().before(now)) {
calendar2.add(Calendar.HOUR, 168);
}
I added the date part today, havent checked it out to know if it will work. Thanks for helping me with this!

How Alaram Service work?
it start as per your condition like you have set for day,week or any week day. so at every specified time set it receive command. like Notification,tone.
so in onStartServiceCommand() method. you have to apply logic for that.
so notification just comes once for that specific day.

Related

android alarmManager setRepating time delay problem

I want to send a notification with the alarm manager in my application. I am using the setRepeating method because it should be in intervals. but notifications always come delayed.
For example, I would like to receive a notification at 12:14. but notification is coming sometimes 12:17, sometimes 12:20
Thanks in advance
Note: doze mode off
For Example:
thatDay Sat Apr 25 16:30:00 GMT+03:00 2020
today Sat Apr 25 16:07:44 GMT+03:00 2020
thatDay.getTimeInMillis() = 1587821400000
#Override
protected void onStop() {
if (notification) {
Calendar thatDay = Calendar.getInstance();
Calendar today = Calendar.getInstance();
int today_month_day = today.get(Calendar.DAY_OF_MONTH);
int today_month = today.get(Calendar.MONTH);
int today_year = today.get(Calendar.YEAR);
thatDay.setTime(new Date(0));
thatDay.set(Calendar.DAY_OF_MONTH, today_month_day);
thatDay.set(Calendar.MONTH, today_month);
thatDay.set(Calendar.YEAR, today_year);
thatDay.set(Calendar.HOUR_OF_DAY, 8);
thatDay.set(Calendar.MINUTE, 0);
thatDay.set(Calendar.SECOND, 0);
thatDay.set(Calendar.MILLISECOND, 0);
Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.HOUR_OF_DAY) < 23 && calendar.get(Calendar.HOUR_OF_DAY) > 7) {
if (calendar.get(Calendar.MINUTE) < 30) {
thatDay.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
thatDay.set(Calendar.MINUTE, 30);
} else {
thatDay.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 1);
}
} else if (thatDay.getTimeInMillis() - today.getTimeInMillis() < 0) {
thatDay.set(Calendar.DAY_OF_MONTH, today_month_day + 1);
} else {
thatDay.set(Calendar.DAY_OF_MONTH, today_month_day);
}
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("NotificationID", 6);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 7, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Objects.requireNonNull(alarmManager).setRepeating(AlarmManager.RTC_WAKEUP, thatDay.getTimeInMillis(), waterList[waterCheck], pendingIntent);
super.onStop();
}
#SuppressLint("BatteryLife")
#RequiresApi(api = Build.VERSION_CODES.M)
private void requestBattery() {
Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (!(Objects.requireNonNull(pm).isIgnoringBatteryOptimizations(packageName))) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivityForResult(intent, BATTERY_PERMISSION);
}
}
This is because setRepeating is only fired, when something else is happening in the background. Mainly this is due to battery saving. The best way to set a repeating alarm is to use setExact and set another alarm once the first one was fired.
Alarmmanager:
public class AlarmManagerSetup {
public static Boolean alarmActivated;
public static Long milliSeconds;
public void startAlarm(Calendar c, Context context, Database database) {
android.app.AlarmManager alarmManager = (android.app.AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, 0);
c.setTimeInMillis(milliSeconds);
//compares to the current time and only adds when it is later than now
if (c.before(Calendar.getInstance())) {
c.add(Calendar.DATE, 1);
}
if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}
}
And inside the Alert Receiver you do this:
public class AlertReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Context appContext = context.getApplicationContext();
CalendarSearch calendar = new CalendarSearch(context);
calendar.calendar();
NotificationHelper notificationHelper = new NotificationHelper(context);
NotificationCompat.Builder nb = notificationHelper.getChannelNotification(appContext);
notificationHelper.getManager().notify(1, nb.build());
AlarmManagerSetup alarmManager = new AlarmManagerSetup();
Calendar c = Calendar.getInstance();
c.setTimeInMillis(milliSeconds);
c.add(Calendar.DATE, 1);
milliSeconds = c.getTimeInMillis();
alarmManager.startAlarm(c, appContext, database);
}

Android How to keep Running the background service when app is killed by user?

I want a background service that keeps running always in a background? It is running on some phone but not running on customised OS phones like VIVO, OPPO MIUI etc? Service is Killed on these customised OS phone.
My Code is given below --
public class MyService extends Service
{
private static final String TAG = "MyService";
#Override
public void onStart(Intent intent, int startId)
{
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
#Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
#Override
public void onCreate()
{
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//call to onTaskRemoved
onTaskRemoved(intent);
//return super.onStartCommand(intent, flags, startId);
Toast.makeText(this, "Service Started!", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
#Nullable
#Override
public IBinder onBind(Intent intent)
{
return null;
}
#Override
public void onDestroy()
{
super.onDestroy();
int i = 1;
Intent intent = new Intent(this, MyBroadCastReceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 5);
if (alarmManager != null)
{
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
60000,
pendingIntent);
}
}
#Override public void onTaskRemoved(Intent rootIntent)
{
Log.e("onTaskRemoved", "Called!");
int i = 1;
Intent intent = new Intent(this, MyBroadCastReceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 5);
if (alarmManager != null)
{
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
pendingIntent);
}
super.onTaskRemoved(rootIntent);
}}
I have written a Broadcast Receiver that wake ups my service for every second but it is not working. My Broadcast Receiver as follows-
public class MyBroadCastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.e("MyBroadCastReceiver", "onReceive");
//if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
{
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
{
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("myFCMJob")
.build();
dispatcher.mustSchedule(myJob);
}
else
{
Intent service = new Intent(context, MyService.class);
context.startService(service);
}
}
}}
I have started my service using Alarm Manager and have set the Alarm for every 5 seconds, my MainActivity.java file code is as below ---
public class MainActivity extends AppCompatActivity
{
Button btnStopService;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStopService = findViewById(R.id.btnStopService);
//get FirebaseToken
getToken();
//start Service
startService();
//setReceiver();
btnStopService.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
stopService(intent);
}
});
}
private void getToken()
{
FirebaseId firebaseId=new FirebaseId();
String token_firebase=firebaseId.getFireBaseToken();
}
private void startService()
{
Intent myIntent = new Intent(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Log.e("TAG", "++++++++++222222++++++++");
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//Calendar calendar = Calendar.getInstance();
// calendar.setTimeInMillis(System.currentTimeMillis());
//calendar.add(Calendar.SECOND, 10);
Calendar alarmStartTime = Calendar.getInstance();
Calendar now = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, 0);
alarmStartTime.set(Calendar.MINUTE, 0);
alarmStartTime.set(Calendar.SECOND, 5);
if (now.after(alarmStartTime)) {
Log.d("Hey","Added a day");
//alarmStartTime.add(Calendar.DATE, 1);
}
if (alarmManager != null) {
alarmManager.set(
AlarmManager.RTC_WAKEUP,
alarmStartTime.getTimeInMillis(),
pendingIntent);
}
Toast.makeText(this, "Start Alarm", Toast.LENGTH_LONG).show();
}}
Thanks in Advance.
To keep your service and all your application running even if the phone goes into sleep mode, your service has to call startForeground() and show a non dismissable notification.
It is also convenient to acquire a partial WAKE LOCK.
on your onStartCommand()
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, Constants.CHANNEL_NOTIFY_ID)
.setContentTitle("the content title")
.setContentText("the content text")
.setSmallIcon(R.drawable.myicon)
.setOngoing(true);
// you can add other options Builder has
startForeground(1234, mBuilder.build());

Android - alarm clock with Notification using AlarmManager

I'm creating an alarm clock using AlarmManager and I have some troubles:
How to stop sounding of notification when I open Notification?
How to make alarm for a certain day of week (or the whole week), not for the certain date?
private void restartNotify()
{
Calendar calendar = Calendar.getInstance();
// current Month - 1
calendar.set(Calendar.MONTH, 5);
calendar.set(Calendar.YEAR, 2015);
calendar.set(Calendar.DAY_OF_MONTH, 28);
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 40);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.PM);
Intent timeIntent = new Intent(MainActivity.this, TimeNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, timeIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
public class TimeNotification extends WakefulBroadcastReceiver
{
#Override
public void onReceive(final Context context, Intent intent)
{
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
//this will send a notification message
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
public class AlarmService extends IntentService
{
private NotificationManager alarmNotificationManager;
public AlarmService() {
super("AlarmService");
}
#Override
public void onHandleIntent(Intent intent)
{
sendNotification("Wake Up! Wake Up!");
}
private void sendNotification(String msg)
{
alarmNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
this).setContentTitle("Alarm").setSmallIcon(R.drawable.abc_btn_radio_material)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
alamNotificationBuilder.setContentIntent(contentIntent);
alarmNotificationManager.notify(1, alamNotificationBuilder.build());
}

Cancelling Alarm Manager in Android

I am having some problem when trying to cancel AlarmManager in Android. I am calling this retrieveBudget() onCreate:
public void retrieveBudget() {
txtDisplayMonthlyExpenses = (TextView) findViewById(R.id.txtDisplayMonthlyExpense);
txtDisplayBudget = (TextView) findViewById(R.id.txtDisplayBudget);
cbDisplayReminderNotify = (CheckBox) findViewById(R.id.cbDisplayReminderNotify);
cbDisplayReminderNotify.setEnabled(false);
DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
TransactionRecController trc = new TransactionRecController(
mDbHelper.open());
currentMonthExpense = trc.getThisMonthDebit();
txtDisplayMonthlyExpenses.setText("$ "
+ formatter.format(currentMonthExpense));
BudgetController bc = new BudgetController(mDbHelper.open());
BudgetModel bm = bc.getBudget();
if (bm.getBudgetAmount() != 0 && bm.getReminderNotify() != null) {
budget = bm.getBudgetAmount();
txtDisplayBudget.setText("$ " + formatter.format(budget));
if (bm.getReminderNotify().equals("Y")) {
cbDisplayReminderNotify.setChecked(true);
} else {
cbDisplayReminderNotify.setChecked(false);
}
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = null;
if (bm.getReminderNotify().equals("Y")
&& currentMonthExpense > budget) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
notificationCount = notificationCount + 1;
Intent notificationIntent = new Intent(context,
BudgetAlarm.class);
notificationIntent.putExtra("NotifyCount", notificationCount);
pi = PendingIntent.getBroadcast(context, notificationCount,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 60000, pi);
} else {
mgr.cancel(pi);
}
} else {
txtDisplayBudget.setText("No budget record yet");
}
mDbHelper.close();
}
And inside my budgetAlarm class, it just simply setting the notification. So my problem is it did execute the notification every minute. But after I changed my reminderNotify to "N" instead of "Y", it does not cancel the alarm manager. I wonder why is it so because after my update SQL statement, I am calling this retrieveBudget() again.
Thanks in advance.
EDIT
These are the codes where I update the budget part. I am calling the the retrieveBudget() once again when the Okay from dialogue box was clicked.
public void onEditBudgetClicked(View view) {
AlertDialog.Builder EditDialog = new AlertDialog.Builder(this);
EditDialog.setTitle("Edit Budget");
// Get the components from XML file
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = li.inflate(R.layout.edit_budget, null);
txtEditBudgetAmount = (EditText) dialogView
.findViewById(R.id.txtEditBudgetAmount);
cbEditReminderNotify = (CheckBox) dialogView
.findViewById(R.id.cbEditReminderNotify);
// Retrieve budget record
DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
BudgetController bc = new BudgetController(mDbHelper.open());
BudgetModel bm = bc.getBudget();
if (bm.getBudgetAmount() != 0 && bm.getReminderNotify() != null) {
txtEditBudgetAmount.setText(Float.toString(bm.getBudgetAmount()));
if (bm.getReminderNotify().equals("Y")) {
cbEditReminderNotify.setChecked(true);
editReminderNotify = "Y";
} else {
cbEditReminderNotify.setChecked(false);
}
}
mDbHelper.close();
cbEditReminderNotify.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (cbEditReminderNotify.isChecked()) {
editReminderNotify = "Y";
} else {
editReminderNotify = "N";
}
}
});
EditDialog.setView(dialogView);
EditDialog.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
onEditBudgetSubmitClicked();
dialog.dismiss();
retrieveBudget();
}
});
EditDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
EditDialog.show();
}
The value of notification count used while initializing the pending intent at the time creation and cancellation of the alarm manager should be same.
Use same value of requestFalg in the pending intent.
Replace this part:
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = null;
if (bm.getReminderNotify().equals("Y")
&& currentMonthExpense > budget) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
notificationCount = notificationCount + 1;
Intent notificationIntent = new Intent(context,
BudgetAlarm.class);
notificationIntent.putExtra("NotifyCount", notificationCount);
pi = PendingIntent.getBroadcast(context, notificationCount,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 60000, pi);
} else {
mgr.cancel(pi);
}
with
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context,
BudgetAlarm.class);
PendingIntent pi = null;
if (bm.getReminderNotify().equals("Y")
&& currentMonthExpense > budget) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
notificationCount = notificationCount + 1;
notificationIntent.putExtra("NotifyCount", notificationCount);
pi= PendingIntent.getBroadcast(context, 1,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 60000, pi);
} else {
pi= PendingIntent.getBroadcast(context, 1,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.cancel(pi);
}
if you changed the value of requestFlag (in your case notificationcount)while initializing the pending intent after launching the alarm, you won't be able to cancel it.
Use same value of requestFlag in the pending intent while creating and cancelling the alarm. Here, I am using 1.
Since you are changing the value of notificationFlag after launching the alarm manager, don't use notificationFlag as a requestFlag. Use a constant integer value as a requestFlag while initializing pi inside both if and else part.
pi= PendingIntent.getBroadcast(context, CONSTANT_INTEGER,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
check the link below:
PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)
http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast%28android.content.Context,%20int,%20android.content.Intent,%20int%29
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = null;
if (bm.getReminderNotify().equals("Y")
&& currentMonthExpense > budget) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
notificationCount = notificationCount + 1;
Intent notificationIntent = new Intent(context,
BudgetAlarm.class);
notificationIntent.putExtra("NotifyCount", notificationCount);
pi = PendingIntent.getBroadcast(context, notificationCount,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 60000, pi);
} else {
notificationCount = notificationCount + 1;
Intent notificationIntent = new Intent(context,
BudgetAlarm.class);
notificationIntent.putExtra("NotifyCount", notificationCount);
pi = PendingIntent.getBroadcast(context, notificationCount,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.cancel(pi);
}

How to send sms frequently like these(daily, weekly, every 5 minutes, every hour)

I want to send sms frequently like that in question; I am using spinner option with some option like that(daily,weekly,every 5 minutes),while i select any option in these spinner it has to go like that.
Alaramsms.java:
private String[] Time_CATEGORY = { "Once","Every hour","Every day", "Weekdays(Mon-Fri)", "Weekend", "Every month",
"Every year" };
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edittextSmsNumber = (EditText)findViewById(R.id.smsnumber);
edittextSmsText = (EditText)findViewById(R.id.smstext);
ImageButton get = (ImageButton)findViewById(R.id.getc);
spinnerTime = (Spinner) findViewById(R.id.spinnerstate);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Time_CATEGORY);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Button buttonStart = (Button)findViewById(R.id.startalarm);
Button buttonCancel = (Button)findViewById(R.id.cancelalarm);
spinnerTime.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
spinnerTime.setSelection(position);
//spinnerCapital.setSelection(position);
String myPrayer = (String) spinnerTime.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> parent) {
}});
get.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(AndroidAlarmSMS.this, ContactActivity.class);
startActivityForResult(i, ResultCode);
}
});
buttonStart.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
smsNumber = edittextSmsNumber.getText().toString();
smsText = edittextSmsText.getText().toString();
picker = new Dialog(AndroidAlarmSMS.this);
picker.setContentView(R.layout.picker_frag);
picker.setTitle("Select Date and Time");
datep = (DatePicker)picker.findViewById(R.id.datePicker);
timep = (TimePicker)picker.findViewById(R.id.timePicker1);
set = (Button)picker.findViewById(R.id.btnSet);
set.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TextUtils.isEmpty(smsNumber))
{
finish();
}
else
{
String[] s =edittextSmsNumber.getText().toString().split(";");
for(int i=0 ;i<s.length;i++)
{
picker.dismiss();
Intent myIntent = new Intent(AndroidAlarmSMS.this, MyAlarmService.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("extraSmsNumber", smsNumber);
bundle.putCharSequence("extraSmsText", smsText);
myIntent.putExtras(bundle);
pendingIntent = PendingIntent.getService(AndroidAlarmSMS.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(datep.getYear(), datep.getMonth(), datep.getDayOfMonth(),
timep.getCurrentHour(), timep.getCurrentMinute(), 0);
// long startTime = calendar.getTimeInMillis();
// Calendar calendar = Calendar.getInstance();
// calendar.setTimeInMillis(System.currentTimeMillis());
// calendar.add(Calendar.SECOND, 60);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(AndroidAlarmSMS.this,
"Start Alarm with \n" +
"smsNumber = " + smsNumber + "\n" +
"smsText = " + smsText,
Toast.LENGTH_LONG).show();
}
}
}
});
picker.show();
}});
buttonCancel.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Toast.makeText(AndroidAlarmSMS.this, "Cancel!", Toast.LENGTH_LONG).show();
}});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ResultCode) {
if(resultCode == RESULT_OK){
sendlist =data.getStringArrayListExtra("name");
if(sendlist!=null)
{
for(int i=0;i<sendlist.size();i++)
{
edittextSmsNumber.append(sendlist.get(i).toString());
edittextSmsNumber.append(";");
}
}
if (resultCode == RESULT_CANCELED) {
}
}
}
}
Now I am sending sms using scheduling time; it is going now at what user select the time.But i want to send sms According to spinner selection.
You need to use setInexactRepeating method
For example, to set alarm "Every Day" :
alarmMgr0 .setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
AlarmManager.INTERVAL_DAY, intent);
This is your AndroidAlarmSMS.Java file.
private String[] Time_CATEGORY = { "Once", "Every 5 Minutes", "Every hour",
"Every day", "Weekly", "Weekdays(Mon-Fri)", "Weekend",
"Every month", "Every year" };
EditText edittextSmsNumber, edittextSmsText;
String smsNumber, smsText;
Dialog picker;
Button select;
Button set;
String mytime;
ImageButton get;
TimePicker timep;
DatePicker datep;
Integer hour, minute, month, day, year, week;
TextView time, date;
private PendingIntent pendingIntent;
static int ResultCode = 12;
ArrayList<String> sendlist = new ArrayList<String>();
Spinner spinnerTime;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edittextSmsNumber = (EditText) findViewById(R.id.smsnumber);
edittextSmsText = (EditText) findViewById(R.id.smstext);
ImageButton get = (ImageButton) findViewById(R.id.getc);
datep = (DatePicker) findViewById(R.id.datePicker);
timep = (TimePicker) findViewById(R.id.timePicker1);
Button buttonStart = (Button) findViewById(R.id.startalarm);
Button buttonCancel = (Button) findViewById(R.id.cancelalarm);
spinnerTime = (Spinner) findViewById(R.id.spinnerstate);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Time_CATEGORY);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTime.setAdapter(adapter_state);
spinnerTime.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
spinnerTime.setSelection(position);
// spinnerCapital.setSelection(position);
mytime = (String) spinnerTime.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
get.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(AndroidAlarmSMS.this,
ContactActivity.class);
startActivityForResult(i, ResultCode);
}
});
buttonStart.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
smsNumber = edittextSmsNumber.getText().toString();
smsText = edittextSmsText.getText().toString();
// picker = new Dialog(AndroidAlarmSMS.this);
// picker.setContentView(R.layout.picker_frag);
// picker.setTitle("Select Date and Time");
// set = (Button)picker.findViewById(R.id.btnSet);
// set.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View view) {
if (TextUtils.isEmpty(smsNumber)) {
finish();
}
else {
String[] s = edittextSmsNumber.getText().toString()
.split(";");
for (int i = 0; i < s.length; i++) {
// picker.dismiss();
Intent myIntent = new Intent(AndroidAlarmSMS.this,
MyAlarmService.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("extraSmsNumber", smsNumber);
bundle.putCharSequence("extraSmsText", smsText);
myIntent.putExtras(bundle);
pendingIntent = PendingIntent.getService(
AndroidAlarmSMS.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// long startTime = calendar.getTimeInMillis();
// Calendar calendar = Calendar.getInstance();
// calendar.setTimeInMillis(System.currentTimeMillis());
// calendar.add(Calendar.SECOND, 60);
if (mytime.equals("Once")) {
Calendar calendar = Calendar.getInstance();
calendar.set(datep.getYear(), datep.getMonth(),
datep.getDayOfMonth(),
timep.getCurrentHour(),
timep.getCurrentMinute(), 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
} else if (mytime.equals("Every 5 Minutes")) {
Calendar calendar = Calendar.getInstance();
calendar.set(datep.getYear(), datep.getMonth(),
datep.getDayOfMonth(),
timep.getCurrentHour(),
timep.getCurrentMinute(), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 60 * 5,
pendingIntent); // Millisec * Second *
// Minute
} else if (mytime.equals("Every hour")) {
Calendar calendar = Calendar.getInstance();
calendar.set(datep.getYear(), datep.getMonth(),
datep.getDayOfMonth(),
timep.getCurrentHour(),
timep.getCurrentMinute(), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 60 * 60,
pendingIntent); // Millisec * Second *
// Minute
} else if (mytime.equals("Every day")) {
Calendar calendar = Calendar.getInstance();
calendar.set(datep.getYear(), datep.getMonth(),
datep.getDayOfMonth(),
timep.getCurrentHour(),
timep.getCurrentMinute(), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
24 * 60 * 60 * 1000, pendingIntent);
} else if (mytime.equals("Weekly")) {
Calendar calendar = Calendar.getInstance();
calendar.set(datep.getYear(), datep.getMonth(),
datep.getDayOfMonth(),
timep.getCurrentHour(),
timep.getCurrentMinute(), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 7 * 24 * 60
* 60 * 1000, pendingIntent);
} else if (mytime.equals("Weekdays(Mon-Fri)")) {
forWeekdays();
} else if (mytime.equals("Weekend")) {
forWeekend();
} else if (mytime.equals("Every month")) {
Calendar calendar = Calendar.getInstance();
calendar.set(datep.getYear(), datep.getMonth(),
datep.getDayOfMonth(),
timep.getCurrentHour(),
timep.getCurrentMinute(), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 30 * 24 * 60
* 60 * 1000, pendingIntent);
} else {
Calendar calendar = Calendar.getInstance();
calendar.set(datep.getYear(), datep.getMonth(),
datep.getDayOfMonth(),
timep.getCurrentHour(),
timep.getCurrentMinute(), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 365 * 24 * 60
* 60 * 1000, pendingIntent);
}
Toast.makeText(
AndroidAlarmSMS.this,
"Start Alarm with \n" + "smsNumber = "
+ smsNumber + "\n" + "smsText = "
+ smsText, Toast.LENGTH_LONG).show();
}
}
}
});
// picker.show();
// }});
buttonCancel.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Toast.makeText(AndroidAlarmSMS.this, "Cancel!",
Toast.LENGTH_LONG).show();
}
});
}
public void forWeekdays() {
Calendar calendar2 = Calendar.getInstance();
calendar2.set(datep.getYear(), datep.getMonth(), datep.getDayOfMonth(),
timep.getCurrentHour(), timep.getCurrentMinute(), 0);
int day = calendar2.get(Calendar.DAY_OF_WEEK);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
if (day == 2 || day == 3 || day == 4 || day == 5 || day == 6) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar2.getTimeInMillis(), 24 * 60 * 60 * 1000,
pendingIntent);
}
}
public void forWeekend() {
Calendar calendar2 = Calendar.getInstance();
calendar2.set(datep.getYear(), datep.getMonth(), datep.getDayOfMonth(),
timep.getCurrentHour(), timep.getCurrentMinute(), 0);
int day = calendar2.get(Calendar.DAY_OF_WEEK);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
if (day == 1 || day == 7) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar2.getTimeInMillis(), 24 * 60 * 60 * 1000,
pendingIntent);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ResultCode) {
if (resultCode == RESULT_OK) {
sendlist = data.getStringArrayListExtra("name");
if (sendlist != null) {
for (int i = 0; i < sendlist.size(); i++) {
edittextSmsNumber.append(sendlist.get(i).toString());
edittextSmsNumber.append(";");
}
}
if (resultCode == RESULT_CANCELED) {
}
}
}
instead of using alarmManager.set(), you can use alarmManager.setRepeating() to repeat the task..
You have to use setRepeating() mmethod of AlarmManager to have this achieved.
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), repeat_interval, pendingIntent);
In the above piece of code you've to replace the value of 'repeat_interval' as per your selection i.e., daily,weekly,every 5 minutes. It denotes the interval at which the alarm repeats. But you've to pass those values in milliseconds.

Categories