I have a Database having various date and time entries. I want to make an android application that matches all the date time entries with the current date and time and gives a notification when the entry matches. What would be the best way to do this? I want that the loop should work every 6 hours. Should I create a service.?
I have a TimeAlarm class
public class TimeAlarm extends BroadcastReceiver {
// NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder nb = new NotificationCompat.Builder(context);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String str = hours + ":" + minutes + ":" + seconds;
System.out.print(str);
TestAdapter tat = new TestAdapter(context);
tat.createDatabase();
tat.open();
Cursor mcurr = tat.getTestData();
if (mcurr != null)
do {
String data = mcurr.getString(4);
if (str.equals(data))
;
{
nb.setContentTitle("New Episode Released");
nb.setContentText(mcurr.getString(1));
nb.setSmallIcon(R.drawable.ic_launcher);
nb.setAutoCancel(true);
NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
final Intent notificationIntent = new Intent(context,
TitleScreen.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
final PendingIntent contentIntent = PendingIntent
.getActivity(context, 0, notificationIntent, 0);
nb.setContentIntent(contentIntent);
Notification notification = nb.getNotification();
nm.notify(0, notification);
}
} while (mcurr.moveToNext());
tat.close();
}
}
Then I have this ShowSeries Class where i have called this TimeAlarm class
public void remind() {
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (5 * 1000), pendingIntent);
}
And this TestAdapter Class through which I am accessing database.
public Cursor getTestData() {
try {
System.out.println("inside gettestdata");
String sql = "SELECT * FROM TV";
System.out.println("query passed");
Cursor mCur = mDb.rawQuery(sql, null);
if (mCur != null) {
mCur.moveToFirst();
}
return mCur;
} catch (SQLException mSQLException) {
Log.e(TAG, "getTestData >>" + mSQLException.toString());
throw mSQLException;
}
}
The remind function is called when I press a specific button. but after that it should work continuously after every 6 hours. After using this code, I am only getting results when I press that button.