Android : How to send Notifications via Service

on Wednesday, September 10, 2014


I am trying to send the notification via service. The time of the notification is set from the database table where I have date, time etc columns. So when System.currentTimeMillis is equal to the time from database column I am trying to send the notification to the user. However my implementation doesn't work, could someone please help me? Thanks in advance


Here is my Service file public class MyService extends Service {



private NotificationManager notificationManager;
private DBHelper dbHelper;
private SQLiteDatabase db;

@Override
public void onCreate() {
Log.i("myLOgs", "Service: onCreate()");
super.onCreate();
dbHelper = new DBHelper(this);
db = dbHelper.getReadableDatabase();
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Log.i("myLOgs", "Service: onStartCommand()");

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String currentDateString = dateFormat.format(date);

SimpleDateFormat timeFormat = new SimpleDateFormat("HH-mm");
Date time = new Date();
String currentTimeString = timeFormat.format(time);

String[] columns = {DBHelper.DATE, DBHelper.TIME, DBHelper.EVENT};
Cursor cursor = db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null);
cursor.moveToFirst();
do {
String dateString = cursor.getString(cursor.getColumnIndex(DBHelper.DATE));
String timeString = cursor.getString(cursor.getColumnIndex(DBHelper.TIME));
String eventString = cursor.getString(cursor.getColumnIndex(DBHelper.EVENT));
if((currentDateString.equals(dateString)) && (currentTimeString.equals(timeString))) {
Notification notification = new Notification(R.drawable.ic_launcher, eventString, System.currentTimeMillis());
}
if(cursor.moveToLast()) {
cursor.moveToFirst();
}
}while(true);

//return super.onStartCommand(intent, flags, startId);
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onDestroy() {
super.onDestroy();
}


}


0 comments:

Post a Comment