'I don't get alarms on my mobile but they do on the android studio emulator
how are you. I have the problem described in the title. The alarms launches correctly in the android emulator but when running it on the mobile they don't launch. It would have to send a toast with the message "salto la alarma" but the message never launches. The structure is as follows: I create in the mainActiviy an object of the Alarma class, which is a class that I have defined, and in that class the AlarmManager should create the alarm, and launch the onReceive of the broadcast receiver. This is the complete code: MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Alarma alarmaMensual=new Alarma(getApplicationContext(),"pastilla","mensual",2022, 2,17,17,41,00,1,1,null);
}
}
Alarma class: (there are many fields that are not used since this is a minimized adaptation of the real application that I have)
public class Alarma {
private int ano;
private int mes;
private int dia;
private int hora;
private int minuto;
private int segundo;
private String pastilla;
private String frecuencia;
private Context contexto;
private int nMinutos;
private int weekOfYear;
private String parteDeldia;
private int requestID;
private AlarmManager aMan;
public Alarma(Context contexto, String pastilla, String frecuencia, int ano, int mes, int dia, int hora, int minuto, int segundo, int nMinutos, int requestID, String parteDelDia) {
this.pastilla=pastilla;
this.frecuencia=frecuencia;
this.ano = ano;
this.mes = mes;
this.dia = dia;
this.hora = hora;
this.minuto = minuto;
this.segundo = segundo;
this.contexto=contexto;
this.nMinutos=nMinutos;
this.parteDeldia=parteDelDia;
this.requestID=requestID;
crearAlarma(contexto,ano,mes,dia,hora,minuto,segundo,requestID);
}
public Calendar crearCalendario(int ano, int mes, int dia, int hora, int minuto, int segundo)
{
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set (Calendar.YEAR, ano);
cal.set (Calendar.MONTH, mes);
cal.set (Calendar.DAY_OF_MONTH, dia);
cal.set (Calendar.HOUR_OF_DAY, hora);
cal.set (Calendar.MINUTE, minuto);
cal.set (Calendar.SECOND, segundo);
return cal;
}
public void crearAlarma(Context contexto,int ano,int mes,int dia,int hora, int minuto,int segundo,int requestID) {
String fecha =ano+"-"+mes+"-"+dia;
Intent intentoLanzar = new Intent(contexto.getApplicationContext(), com.example.pruebaalarma.Temporizador.class);
intentoLanzar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Calendar calendario= crearCalendario(ano,mes,dia,hora,minuto,segundo);
PendingIntent pIntent=PendingIntent.getBroadcast(contexto, requestID, intentoLanzar, 0);
aMan = (AlarmManager)contexto.getSystemService(Context.ALARM_SERVICE);
//aMan.set(AlarmManager.RTC_WAKEUP, calendario.getTimeInMillis(), pIntent);
aMan.setRepeating(AlarmManager.RTC_WAKEUP, calendario.getTimeInMillis(), 1000*60*nMinutos, pIntent);
//Toast.makeText(contexto, "hola", Toast.LENGTH_LONG).show();
}
}
this is the broadcast receiver:
public class Temporizador extends BroadcastReceiver {
private Context contexto;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context.getApplicationContext(),"salto la alarma", Toast.LENGTH_LONG).show();
}
}
and this is the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pruebaalarma">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PruebaAlarma">
<receiver
android:name=".Temporizador"
android:process=":remote" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Solution 1:[1]
i managed to solve it, setting the application to "without restrictions" in battery settings.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | guillanBesada |
