I am trying to create a task to send a notification everyday at 1:00 am. the task should run even if the application is off.
I have used Service and AlarmManager to do the job. But the problem is that the task is executed once and whenever the application gets on. I guess that the service is killed for resources allocation.
my question is how to implement the schedule task to keep working and activate itself even if it is killed, and how to get the task schedule at the time of app installation. currently, I put the initiation at main activity.
Here a snippets from the code: I appreciate any help regarding this:
public class MyAlarmService extends Service{
private NotificationManager mManager;
@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 int onStartCommand(final Intent intent, final int flags,
final int startId) {
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),TabsFragmentActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT );
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = " Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
PendingIntent pendingIntent =PendingIntent.getActivity(this, 0,Intent.createChooser(sharingIntent, getResources().getString(R.string.choose_share_action)),
PendingIntent.FLAG_UPDATE_CURRENT);
String msgText = "Notification long text example!! "
+ "where you will see three different kind of notification. "
+ "you can even put the very long string here.";
int icon = R.drawable.ic_launcher;
Builder builder = new Notification.Builder(this);
builder.setContentTitle(getResources().getString(R.string.noticifcation_content_title))
.setContentText("This is the compact version")
.setContentIntent(pendingNotificationIntent)
.setTicker(getResources().getString(R.string.noticifcation_ticker))
.setSmallIcon(icon)
.setAutoCancel(false)
.setPriority(Notification.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_action_share,
getResources().getString(R.string.share),pendingIntent);
Notification notification = new Notification.BigTextStyle(builder)
.bigText(msgText).build();
mManager.notify(0, notification);
}
Receiver:
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MyAlarmService.class);
context.startService(service);
}
}
private void createNotification() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
Intent myIntent = new Intent(TabsFragmentActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(TabsFragmentActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES , pendingIntent);
}