first of all sorry for my horrible english, this is my problem:
I have a MainActivity.class that set the alarmmanager, at the specific time my alarmReceiver check on database if there is a specific record. If the check result false, he start the service that show my notification.
The notification open my MealActivity.class for give user's possibilities to insert a record on database (the same of alarmReceiver's check) and set a new alarm (at least +5 hours).
The problem begin when, on my MealActivity.class, without insert a record, i click on "up button" on my toolbar to back on father activity (MainActivity.class) and here, in MainActivity, i receive a new notification, same as the previous. But why should not because the next notification for the alarm has been set to at least five hours later, more than a few seconds.
Why?
In my MealActivity, if i press "up button", i want to come back to MainActivity without notification. The next notification should appear at the appointed time.
How can i resolve this?
MainActivity.class:
public class MainActivity extends AppCompatActivity {
private AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/**
* Set alarm for food
*/
Calendar calendar = new GregorianCalendar();
int mealType;
if((calendar.get(Calendar.HOUR_OF_DAY) >= 0 && calendar.get(Calendar.HOUR_OF_DAY) < 11) || (calendar.get(Calendar.HOUR_OF_DAY) >= 23 && calendar.get(Calendar.HOUR_OF_DAY) < 24)){
mealType = 0;
}
else if(calendar.get(Calendar.HOUR_OF_DAY) >= 11 && calendar.get(Calendar.HOUR_OF_DAY) < 16){
mealType = 1;
}
else mealType = 2;
setAlarm(mealType);
}
public void setAlarm(int mealType){
/**
* Set standard alarm.
*/
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(MainActivity.this, AlarmReceiver.class);
i.putExtra("mealType", mealType);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, i, PendingIntent.FLAG_ONE_SHOT);
Calendar c = new GregorianCalendar();
if(mealType == 0 && c.get(Calendar.HOUR_OF_DAY) >= 23) {
c.add(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.HOUR_OF_DAY, 11);
}
else if(mealType == 0 && c.get(Calendar.HOUR_OF_DAY) < 11) {
c.set(Calendar.HOUR_OF_DAY, 11);
}
else if(mealType == 1){
c.set(Calendar.HOUR_OF_DAY, 16);
}
else c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
}
// other stuff..
}
My AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("ALARM RECEIVER", "Allarm received");
int mealType = intent.getExtras().getInt("mealType");
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(context);
databaseAccess.open();
int userID = databaseAccess.getActualID();
boolean thereIsAMeal = databaseAccess.thereIsAMeal(userID);
databaseAccess.close();
Intent service1 = new Intent(context, AlarmService.class);
if(!thereIsAMeal) {
Log.d("ALARM RECEIVER", "Start service");
service1.putExtra("mealType", mealType);
context.startService(service1);
}
}
}
My AlarmService:
public class AlarmService extends IntentService {
private static final int NOTIFICATION_ID = 1;
private static final String TAG = "MEALALARM";
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
public AlarmService() {
super("AlarmService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent,flags,startId);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "Alarm Service has started.");
Context context = this.getApplicationContext();
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
int mealType = intent.getExtras().getInt("mealType");
Intent mIntent = new Intent(this, MealActivity.class);
mIntent.putExtra("origin", "notify");
mIntent.putExtra("mealType", mealType);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_ONE_SHOT);
Resources res = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.icon))
.setTicker(res.getString(R.string.notification_title))
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.notification_title))
.setContentText(res.getString(R.string.notification_subject_food))
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Log.i(TAG, "Notifications sent.");
stopSelf();
}
}
MealActivity:
public class MealActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
Intent intent = getIntent();
if (intent.getExtras().getString("origin").compareTo("notify") == 0) {
int mealType = intent.getExtras().getInt("mealType");
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
int actualID = databaseAccess.getActualID();
databaseAccess.close();
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(MealActivity.this, AlarmReceiver.class);
mealType = nextMeal(mealType);
i.putExtra("mealType", mealType);
PendingIntent pi = PendingIntent.getBroadcast(MealActivity.this, 0, i, PendingIntent.FLAG_ONE_SHOT);
Calendar c = Calendar.getInstance();
if(mealType == 0 && c.get(Calendar.HOUR_OF_DAY) >= 23) {
c.add(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.HOUR_OF_DAY, 11);
}
else if(mealType == 0 && c.get(Calendar.HOUR_OF_DAY) < 11) {
c.set(Calendar.HOUR_OF_DAY, 11);
}
else if(mealType == 1){
c.set(Calendar.HOUR_OF_DAY, 16);
}
else c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
}
} catch (NullPointerException ex){
}
// other stuff...
}
}
Thanks for your help.