Why are notifications not shown at the proper time in my android project?
<p>I've done an app that in a certain time it sends a notification to acces an other activity, but when I set the time the notification appears only if I shut the app and in the time it wants. Someone knows how to change my code to make it appear at the selected hour? Even if the app is still in the background.</p>

<p>My activity:</p>

<pre><code>package app.alexdickson.com.workout1;

import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;


public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
ImageButton botoFlexio;
ImageButton botoAbdominals;
static final int DIALOG_ID = 0;
int hour_x;
int minute_x;
int hourDefinitivaFlexio;
int minuteDefinitvaFlexio;
int hourDefinitivaAbs;
int minuteDefinitivaAbs;
PendingIntent pendingIntent1;
PendingIntent pendingIntent2;
Context context;
AlarmManager alarmManager1;
AlarmManager alarmManager2;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    this.context = this;
    botoFlexio = (ImageButton) findViewById(R.id.botoFlexio);
    botoAbdominals = (ImageButton) findViewById(R.id.botoAbdominals);
    botoFlexio.setOnClickListener(this);
    botoAbdominals.setOnClickListener(this);
    alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);


}
 private void alarmMethod1(){
    Intent myIntent1 = new Intent(this, NotifyService1.class);
    AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent1 =PendingIntent.getService(this,0,myIntent1,0);

    Calendar calendari1 = Calendar.getInstance();
    calendari1.set(Calendar.MINUTE, minute_x);
    calendari1.set(Calendar.HOUR,hour_x);

    alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,calendari1.getTimeInMillis(),
            1000 * 60 * 60 * 24, pendingIntent1);


}


private void alarmMethod2(){
    Intent myIntent2 = new Intent(this, NotifyService2.class);
    AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent2 =PendingIntent.getService(this,0,myIntent2,0);

    Calendar calendari2 = Calendar.getInstance();
    calendari2.set(Calendar.MINUTE,minute_x);
    calendari2.set(Calendar.HOUR, hour_x);

    alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,calendari2.getTimeInMillis(),
            1000 * 60 * 60 * 24, pendingIntent2);


}










@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.botoFlexio:
            botoFlexio.setBackgroundResource(R.drawable.flexioclicat);
            showDialog(DIALOG_ID);
            hourDefinitivaFlexio = hour_x;
            minuteDefinitvaFlexio = minute_x;

            alarmMethod1();

            break;

        case R.id.botoAbdominals:
            botoAbdominals.setBackgroundResource(R.drawable.abdominalsclicat);
            showDialog(DIALOG_ID);
            hourDefinitivaAbs = hour_x;
            minuteDefinitivaAbs = minute_x;

            alarmMethod2();

            break;

    }
}

@Override
protected Dialog onCreateDialog(int id) {
    if (id == DIALOG_ID)

        return new TimePickerDialog(Main2Activity.this, kTimePickerListener, hour_x, minute_x, true);
    return null;
}

protected TimePickerDialog.OnTimeSetListener kTimePickerListener =
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                hour_x = hourOfDay;
                minute_x = minute;
                Toast.makeText(Main2Activity.this, hour_x + ": " + minute_x, Toast.LENGTH_LONG).show();
            }
        };
</code></pre>

<p>Alarm Receiver:(There's 2 but they are the same, the only thing that changes is the numbers)</p>

<pre><code>package app.alexdickson.com.workout1;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;



public class NotifyService1 extends Service {

@Override
public IBinder onBind(Intent intent) {
    return null;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onCreate(){
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


    NotificationManager nNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),Main3Activity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);

    Notification mNotify = new Notification.Builder(this)
            .setContentTitle("Hora d'entrenar!")
            .setContentText("Clica per començar entrenament de pit i braços")
            .setSmallIcon(R.drawable.logofinal)
            .setContentIntent(pIntent)
            .setSound(sound)
            .build();

    nNM.notify(1,mNotify);

}
</code></pre>

<p>}</p>