I want to excute a method in my dbhelper immediately my alarm service run, the if that code runs successfully I want to display a notification based on the result of the method execution. I want this to run even if my app is idle or in the background
this is my broadcastReceiver
public class ExpiryBroadcast extends BroadcastReceiver {
//db helper
private DbHelper dbHelper;
private final String CHANNEL_ID = "expiring_items";
private final int NOTIFICATION_ID = 200;
#Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
dbHelper.updateExpiryRow();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_item_expiring)
.setContentTitle("ProExm Product Expiry")
.setContentText("Some products will soon expire, check now...")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
}
this is my alarmmanager
public void startAlertAtParticularTime() {
// alarm first vibrate at 14 hrs and 40 min and repeat itself at ONE_HOUR interval
intent = new Intent(this, ExpiryBroadcast.class);
pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 280192, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 02);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(this, "Alarm will vibrate at time specified",
Toast.LENGTH_SHORT).show();
}
I have this code in my dbhelper class which I want to run before notification even if my application is asleep or in the background
public void updateExpiryRow(){
int daysToExpiry = 0;
String selectQuery = "SELECT * FROM " + Constants.ITEMS_TABLE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//looping through all records and add to list
if(cursor.moveToFirst()){
do{
ModelItems modelItems = new ModelItems(
""+cursor.getInt(cursor.getColumnIndex(Constants.C_ID)),
""+cursor.getString(cursor.getColumnIndex(Constants.C_ITEM_NAME)),
""+cursor.getString(cursor.getColumnIndex(Constants.C_ITEM_IMAGE)),
""+cursor.getString(cursor.getColumnIndex(Constants.C_ITEM_PRICE)),
""+cursor.getString(cursor.getColumnIndex(Constants.C_ITEM_MANUFACTURER)),
""+cursor.getString(cursor.getColumnIndex(Constants.C_DESC)),
""+cursor.getString(cursor.getColumnIndex(Constants.C_EXPIRY_DATE)),
""+cursor.getString(cursor.getColumnIndex(Constants.C_MANUFACTURE_DATE)),
Integer.parseInt(""+cursor.getInt(cursor.getColumnIndex(Constants.C_DAYS_TO_EXPIRY)))-1,
""+cursor.getString(cursor.getColumnIndex(Constants.C_ADDED_TIMESTAMP)),
""+cursor.getString(cursor.getColumnIndex(Constants.C_UPDATED_TIMESTAMP))
);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd-MM-yyyy");
try {
Date date = formatter.parse(modelItems.getItemExp());
LocalDate now = LocalDate.now();
String text = now.format(formatter2);
Date dateNow = formatter.parse(text);
long diffInMillies = Math.abs(date.getTime() - dateNow.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
daysToExpiry = (int) (long) diff;
} catch (ParseException e) {
e.printStackTrace();
}
ContentValues values = new ContentValues();
// id will be inserted automatically as we set AUTOINCREMENT in query
//insert data
String timestamp = ""+System.currentTimeMillis();
values.put(Constants.C_ID, modelItems.getId());
values.put(Constants.C_ITEM_NAME, modelItems.getItemName());
values.put(Constants.C_ITEM_IMAGE, modelItems.getItemImage());
values.put(Constants.C_ITEM_PRICE, modelItems.getItemPrice());
values.put(Constants.C_ITEM_MANUFACTURER, modelItems.getItemManufacturer());
values.put(Constants.C_DESC, modelItems.getItemDesc());
values.put(Constants.C_EXPIRY_DATE, modelItems.getItemExp());
values.put(Constants.C_MANUFACTURE_DATE, modelItems.getItemMfd());
values.put(Constants.C_DAYS_TO_EXPIRY, daysToExpiry);
values.put(Constants.C_ADDED_TIMESTAMP, modelItems.getAddedTime());
values.put(Constants.C_UPDATED_TIMESTAMP, timestamp);
//insert row, it will return record id of saved record
db.update(Constants.ITEMS_TABLE, values, Constants.C_ID +" = ?", new String[] {modelItems.getId()});
//add record to list
}while (cursor.moveToNext());
}
//close db connection
db.close();
}
First of all you have to initialize the dbHelper
dbHelper = new DbHelper();
Then just call your method.
And this broadcast receiver will work if your app is idle or even closed
Try to use a foreground service and start it from your broadcast receiver, it will work even if the app is killed, and in your service, you can instantiate your DbHelper and execute what you want, after finishing tasks just stop it.
Related
I have a menu case that allows me to set a notification trigger based on the "term" start date. This takes user input where they select the start date for a term and creates a notification alert based on their choice:
case R.id.notify:
String startDateFromScreen = editTextStartDate.getText().toString();
String endDateFromScreen = editTextEndDate.getText().toString();
Date startDate = null;
Date endDate = null;
try {
startDate = sdf.parse(startDateFromScreen);
endDate = sdf.parse(endDateFromScreen);
} catch (ParseException e) {
e.printStackTrace();
}
Intent intent = new Intent(AddEditTermActivity.this,
MyReceiver.class);
Long triggerStartDate = startDate.getTime();
Long triggerEndDate = endDate.getTime();
intent.putExtra("start", editTextTermTitle.getText().toString() + " starts today!");
intent.putExtra("end", editTextTermTitle.getText().toString() + " ends today!");
PendingIntent startSender = PendingIntent.getBroadcast(AddEditTermActivity.this, MainActivity.termStartAlertNum++, intent, 0);
PendingIntent endSender = PendingIntent.getBroadcast(AddEditTermActivity.this, MainActivity.termEndAlertNum++, intent, 0);
AlarmManager startAlerm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
startAlerm.set(AlarmManager.RTC_WAKEUP, triggerStartDate, startSender);
return true;
I also want to be able to create a separate notification based on their selected end time, as you can see from some of the code in that section. This intent is sent to a receiver class:
public class MyReceiver extends BroadcastReceiver {
String startChannelID = "test";
static int notificationID;
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, intent.getStringExtra("start"), Toast.LENGTH_LONG).show();
createNotificationChannel(context, startChannelID);
Notification start = new NotificationCompat.Builder(context, startChannelID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentText(intent.getStringExtra("start"))
.setContentTitle("Starting").build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(notificationID++, start);
}
private void createNotificationChannel(Context context, String CHANNEL_ID) {
CharSequence name = context.getResources().getString(R.string.channel_name);
String description = context.getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
But when creating a separate notification/notification manager, it only shows the last notification line in the code. How can I make it so that both notifications are set on the respective dates? Time does not matter in this example.
I have created an app, that allows a user to input assignment deadlines and then a time to be notified that the assignment is due, this all works fine, however, I can't find a clear example of how to make the alarms survive a reboot.
The database stuff on the receiver is just to populate the notification with the specific assignments information. The alarms themselves are not saved in the database, the times and dates are set by user input and directly passed to alarm manager.
Alarm creation page:
Long nIdLong = System.currentTimeMillis();
String nId = nIdLong.toString();
final int _id = (int) System.currentTimeMillis();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("nID", nIdLong);
// make sure the intent have id's
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, _id, alarmIntent , 0);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
This is my receiver:
public void onReceive(Context context, Intent intent)
{
long dbId = intent.getLongExtra("nID", 0);
String notificationId = Long.toString(dbId);
Log.i("App", "called receiver method");
try{
userDbHelper = new UserDbHelper(context.getApplicationContext());
sqLiteDatabase = userDbHelper.getReadableDatabase();
cursor = userDbHelper.getAssignmentNotification(notificationId, sqLiteDatabase);
if (cursor.moveToFirst()) {
do {
id = cursor.getString(0);
name = cursor.getString(1);
subject = cursor.getString(2);
date = cursor.getString(3);
time = cursor.getString(4);
} while (cursor.moveToNext());
}
NotificationGenerator.generateNotification(context, date, time, name, subject);
}catch(Exception e){
e.printStackTrace();
}
}
https://examples.javacodegeeks.com/android/core/activity/android-start-service-boot-example/
This example shows how to start services on boot, save the alarms into a database and have the on boot service run through all the saved alarms to have them recreated upon each boot.
I've created a notification and it's just doesnt show up.
My code in the main activity: `boolean alarm = (PendingIntent.getBroadcast(this, 0, new Intent("ALARM"), PendingIntent.FLAG_NO_CREATE) == null);
if(alarm){
Intent itAlarm = new Intent("ALARM");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,itAlarm,0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 3);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
alarme.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),60000, pendingIntent);
}
my code in the Broadcast Reciver:
public class BroadcastManager extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
String yourDate = "04/05/2016";
String yourHour = "13:07:00";
Date d = new Date();
DateFormat date = new SimpleDateFormat("dd/MM/yyyy");
DateFormat hour = new SimpleDateFormat("HH:mm:ss");
if (date.equals(yourDate) && hour.equals(yourHour)){
Intent it = new Intent(context, MainActivity.class);
createNotification(context, it, "new mensage", "body!", "this is a mensage");
}
}catch (Exception e){
Log.i("date","error == "+e.getMessage());
}
}
public void createNotification(Context context, Intent intent, CharSequence ticker, CharSequence title, CharSequence descricao){
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent p = PendingIntent.getActivity(context, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setTicker(ticker);
builder.setContentTitle(title);
builder.setContentText(descricao);
builder.setSmallIcon(R.drawable.web_hi_res_512);
builder.setContentIntent(p);
Notification n = builder.build();
//create the notification
n.vibrate = new long[]{150, 300, 150, 400};
n.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(R.drawable.web_hi_res_512, n);
//create a vibration
try{
Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone toque = RingtoneManager.getRingtone(context, som);
toque.play();
}
catch(Exception e){}
}
}
Above code will start alarm service on every 3 seconds and repeat on every 1 minute. If you want to generate notification on specific time then, You have to add,
calendar.add(Calendar.HOUR, 13);
calendar.add(Calendar.MINUTES, 07);
calendar.add(Calendar.SECONDS, 00);
and remove this line,
calendar.add(Calendar.SECOND, 3);
I hope it may work for you.
I try to have a notification repeated daily (For debugging I set it to every 10s). However, it is firing the notification only the first time, then nothing happens.
Here is the code where the alarm is set:
Intent myIntent = new Intent(ctx , NotifyService.class);
AlarmManager alarmManager =(AlarmManager)ctx.getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(ctx, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 1000 * 10, pendingIntent);
and here is the service:
public class NotifyService extends Service {
public NotifyService() {
}
#Override
public void onCreate(){
//Create and Emit the notification.
}
I have tried different flags in getService(ctx, int, Intent, flags), to use setInexactRepeating and to set a new alarm after every call to the NotifyService.
Use the method below to repeating the alarm once in a day and you have to register broadcast receiver instead of service with AlarmManager so that you can start your service from the receiver and that is recommended.
Find the official doc.
private final static String ACTION = "ACTION_ALARM";
public static void setWakeUpAction(Context context, String hourSet, String minuteSet, String periodSet, int requestCode, String currentAction) {
try {
String mHour = hourSet;
String mMin = minuteSet;
String[] parsedFormat = null;
Calendar calendar = Calendar.getInstance();
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
Date date = parseFormat.parse(mHour + ":" + mMin + " " + periodSet);
parsedFormat = displayFormat.format(date).split(":");
mHour = parsedFormat[0];
mMin = parsedFormat[1];
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(mHour));
calendar.set(Calendar.MINUTE, Integer.parseInt(mMin));
calendar.set(Calendar.SECOND, 00);
Intent myIntent = new Intent(context, MyReceiver.class);
myIntent.putExtra(ACTION, currentAction);
myIntent.putExtra("Time", new String[]{mHour, mMin, periodSet});
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
} catch (ParseException e) {
e.printStackTrace();
}
}
BroadCastReceiver
public class MyReceiver extends BroadcastReceiver {
private final String ACTION = "ACTION_ALARM";
private String ACTION_ONE = "ALARM_REPEAT";
#Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getStringExtra(ACTION);
new ShowToast(context, action);
if (action.length() > 1) {
if (action.equals(ACTION_ONE) ) {
String time[] = intent.getStringArrayExtra("Time");
startService(context, action);
}
}
} catch (Exception e) {
}
}
public void startService(Context context, String action) {
Intent service1 = new Intent(context, NotifyService.class);
service1.putExtra(ACTION, action);
context.startService(service1);
}
}
Manifest
<service
android:name=".NotifyService"
android:enabled="true" />
<receiver android:name=".MyReceiver" />
Usage
setWakeUpAction(context, "11", "00","AM","0", "ALARM_REPEAT");
Use a PendingIntent for a BroadcastReceiver rather than a Service, that is the recommended (and documented) practice. Plus, if you are using "wakeup" alarms, you'll need to use a BroadcastReceiver otherwise the system will not guarantee that it stays awake long enough for your Service to actually execute and receive the Intent. See this article for more details: http://po.st/7UpipA
Try this,
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// setRepeating() lets you specify a precise custom interval--in this case,
// 10 seconds.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 10, alarmIntent);
Refer : http://developer.android.com/training/scheduling/alarms.html
I am having some problem when trying to prompt notification based on the timing set by Alarm Manager in Android.
So what I am trying to do is a budget setting application. If the monthly expenses exceed the budget, it will prompt notification. In order to keep the question short, I will just post the part where I interact with Alarm Manager:
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context, BudgetAlarm.class);
PendingIntent pi = null;
if (bm.getReminderNotify().equals("Y")
&& percentage >= 90) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
notificationCount = notificationCount + 1;
// Set request flag to 1 so that the same pending intent in broadcastReceiver
notificationIntent.putExtra("NotifyCount", notificationCount);
pi = PendingIntent.getBroadcast(context, 1,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
What I am trying to do here is to check if the expenses exceed the budget. And inside the BudgetAlarm class, I am prompting the notification:
private NotificationManager mNotificationManager;
private Notification notification;
#Override
public void onReceive(Context context, Intent intent) {
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(context, 1,
new Intent(), 0);
notification = new Notification(R.drawable.ic_launcher, "Notification",
System.currentTimeMillis());
notification.setLatestEventInfo(context, "Budget Reminder",
"Your monthly expenses nearly exceed the budget!",
contentIntent);
mNotificationManager.notify(
Integer.parseInt(intent.getExtras().get("NotifyCount")
.toString()), notification);
}
My problem now is it does set and trigger the alarm manager every day. But let's say today I did not run the apps, it does prompt me a notification to notify me. But after a while, I launch the app, then it prompt me again.
I wonder is there any way to set the notification to just notify once per day regardless of how many times I launch the apps. I was actually thinking of something like cookies or shared preference.
Thanks in advance.
EDIT
public class BudgetAlarm extends BroadcastReceiver {
private NotificationManager mNotificationManager;
private Notification notification;
#Override
public void onReceive(Context context, Intent intent) {
saveDay(context);
if (sameDay(context) == false) {
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(context, 1,
new Intent(), 0);
notification = new Notification(R.drawable.ic_launcher,
"Notification", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Budget Reminder",
"Your monthly expenses nearly exceed the budget!",
contentIntent);
mNotificationManager.notify(
Integer.parseInt(intent.getExtras().get("NotifyCount")
.toString()), notification);
saveDay(context);
}
}
private boolean sameDay(Context context) {
boolean isSameDay = false;
SharedPreferences pref = context.getSharedPreferences("PrefKey",
Context.MODE_PRIVATE);
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
Date today = cal.getTime();
String day = format.format(today);
String savedDate = pref.getString("SaveDateKey", "NONE");
if (savedDate.equals(day)) {
isSameDay = true;
} else {
isSameDay = false;
}
return isSameDay;
}
private void saveDay(Context context) {
SharedPreferences pref = context.getSharedPreferences("PrefKey",
Context.MODE_PRIVATE);
Editor editor = pref.edit();
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
Date today = cal.getTime();
String day = format.format(today);
editor.putString("SaveDateKey", day);
editor.commit();
}
}
I think You are on the right way with shared preferences. This could be a scenario in Your app: make a method to know if the same day is equal, if not, save the date when You start the app:
private boolean sameDay(){
boolean isSameDay = false;
SharedPreferences pref = getApplicationContext().getSharedPreferences(YOUR_PREF_KEY, Context.MODE_PRIVAT);
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
Date today = cal.getTime();
String day = format.format(today);
String savedDate = pref.getString(YOUR_SAVED_DATE_KEY,"NONE");
if(savedDate.equals(day){
isSameDay=true;
}else{
isSameDay=false;
}
return isSameDay;
}
And to save the current day:
private void saveDay(){
SharedPreferences pref = getApplicationContext().getSharedPreferences(YOUR_PREF_KEY, Context.MODE_PRIVAT);
Editor editor = pref.edit();
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
Date today = cal.getTime();
String day = format.format(today);
editor.putString(YOUR_SAVED_DATE_KEY,day);
editor.commit();
}
And then You just handling this inside Your onReceive():
if(sameDay==true){
//donĀ“t notify
}else{
//notify and save the day
saveDay();
}