Reputation: 421
I want to make local notifications that appears only 1st, 2nd and 3rd day of the month at for example 11am. I set up my notifications for 3 days in row at 12:30, 14:30 and 18:30 but it shown only once, at 14:30. When I checked android monitor for that hour it looks like android just skip that sec?! Here is my code:
here is my service which is setup in main activity in onCreate
`@EService
public class NotifiService extends Service {
public int counter = 0;
public NotifiService(Context applicationContext) {
super();
Log.w("here!", "here i am");
}
public NotifiService(){
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent,flags,startId);
Log.i("-----b", "onStartCommand");
startTimer();
return Service.START_STICKY; //super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Intent broadCastIntent = new Intent("com.Yyyyy.Xxxxx.Yyyyy.BootBro");
sendBroadcast(broadCastIntent);
stopTimerTask();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Background
public void checkDate(){
Log.w("f", "if working");
Calendar cal = Calendar.getInstance();
int d = cal.get(Calendar.DAY_OF_MONTH);
int h = cal.get(Calendar.HOUR_OF_DAY);
int s = cal.get(Calendar.SECOND);
int m = cal.get(Calendar.MINUTE);
if( d==17 || d == 18 || d == 19){
if (h == 12 || h == 14 || h == 16) {
if (m == 30) {
if (s == 30) {
Log.w("notify", "working>>>>");
Intent intent = new Intent(getApplicationContext(), MainActivity_.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(getApplicationContext());
b.setAutoCancel(false)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_app_icon)
.setTicker("FabReminder")
.setContentTitle("FabReminder")
.setContentText(getString(R.string.notification))
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent);
//.setContentInfo("Tap this bar and select shop");
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
}
}
}
}
private Timer timer;
private TimerTask timerTask;
long oldTime=0;
public void stopTimerTask(){
if (timer != null){
timer.cancel();
timer = null;
}
}
public void initializeTimerTask(){
timerTask = new TimerTask() {
@Override
public void run() {
checkDate();
}
};
}
public void startTimer(){
timer = new Timer();
initializeTimerTask();
timer.schedule(timerTask,1000,1000);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.w("killed task >>>"," task killed");
Intent broadCastIntent = new Intent("com.Yyyyy.Xxxxx.Yyyyy.BootBro");
sendBroadcast(broadCastIntent);
}`
Here is my BroadcastReceiver:
`public class BootBro extends BroadcastReceiver { public int MID;
@Override
public void onReceive(Context context, Intent intent) {
//context.startService(new Intent(context, NotifiService_.class));
Log.w(BootBro.class.getSimpleName(), "Service stops!");
context.startService(new Intent(context, NotifiService_.class));
}
}`
and AndroidManifest
`<service android:name="com.Yyyyy.Xxxx.Yyyyy.NotifiService_" android:enabled="true"/>
<receiver android:name="com.Yyyyy.Xxxx.Yyyy.BootBro" android:enabled="true"
android:exported="true"
android:label="RestartServiceWhenStopped">
<intent-filter><action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<action android:name="com.Yyyyy.Xxxx.Yyyy.BootBro"/>
</intent-filter>
</receiver>`
Upvotes: 2
Views: 827
Reputation: 879
This approach you can setup easy with a AlarmManager.
This class provides access to the system alarm services.These allow you to schedule your application to be run at some point in the future... More
Alarms have these characteristics:
The official Android documentation have a tutorial that you can follow here
And also here is an example with a simple app that you can setup an alarm to wake up.
Upvotes: 1