I want every day at 10 pm to get a notification to the user. When 22 hours arrive, nothing happens. follow the code below
public void Alarme(){
this.context = this;
Intent myIntent = new Intent(this.context, AlarmReceiver.class);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 22);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.PM);
myIntent.putExtra("extra", "yes");
pending_intent = PendingIntent.getBroadcast(com.jbu.olamundo.olamundo.TelaPrincipal.this, 0, myIntent, FLAG_UPDATE_CURRENT);
if (calendar.before(Calendar.getInstance())){
calendar.add(Calendar.DATE, 1);
}
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
pending_intent);
}
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getExtras().getString("extra");
Log.e("MyActivity", "In the receiver with " + state);
Intent serviceIntent = new Intent(context, Som.class);
serviceIntent.putExtra("extra", state);
context.startService(serviceIntent);
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
serviceIntent.putExtra("extra", state);
context.startService(serviceIntent);
}
}
}
public class Som extends Service {
private boolean isRunning;
MediaPlayer mMediaPlayer;
private int startId;
private String nomedoAssistente;
public String oi;
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e("MyActivity", "In the Assistente service");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Preferencias preferencias = new Preferencias(Som.this);
nomedoAssistente = preferencias.getNomeDoAssistente();
SharedPreferences sharedPreferencesdx = getSharedPreferences(ARQUVO_MENSAGEMASS, 0);
SharedPreferences.Editor editdx = sharedPreferencesdx.edit();
if (sharedPreferencesdx.contains("INGLES")){
oi = "HELLO WORLD!!!!!!!!!!!";
}else{
oi = "OLÁ MUNDO!!!!!!!!!!!";
}
final NotificationManager mNM = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(), TelaPrincipal.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification mNotify = new Notification.Builder(this)
.setContentTitle(nomedoAssistente + ":")
.setContentText(oi)
.setSmallIcon(R.drawable.ciencia)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
String state = intent.getExtras().getString("extra");
Log.e("what is going on here ", state);
assert state != null;
switch (state) {
case "no":
startId = 0;
break;
case "yes":
startId = 1;
break;
default:
startId = 0;
break;
}
if(!this.isRunning && startId == 1) {
Log.e("if there was not sound ", " and you want start");
mMediaPlayer = MediaPlayer.create(this, R.raw.notificacao);
mMediaPlayer.start();
mNM.notify(0, mNotify);
this.isRunning = true;
this.startId = 0;
}
else if (!this.isRunning && startId == 0){
Log.e("if there was not sound ", " and you want end");
this.isRunning = false;
this.startId = 0;
}
else if (this.isRunning && startId == 1){
Log.e("if there is sound ", " and you want start");
this.isRunning = true;
this.startId = 0;
}
else {
Log.e("if there is sound ", " and you want end");
mMediaPlayer.stop();
mMediaPlayer.reset();
this.isRunning = false;
this.startId = 0;
}
Log.e("MyActivity", "In the service");
return START_STICKY;
}
@Override
public void onDestroy() {
Log.e("JSLog", "on destroy called");
super.onDestroy();
this.isRunning = false;
}
public class BootService extends IntentService {
private PowerManager.WakeLock wakeLock;
private String nomedoAssistente;
private String nomedoHumano;
public String oi;
public BootService() {
super("name");
}
@Override
public void onCreate() {
super.onCreate();
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "BootService");
wakeLock.acquire();
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Preferencias preferencias = new Preferencias(BootService.this);
nomedoAssistente = preferencias.getNomeDoAssistente();
nomedoHumano = preferencias.getNomeDoHumano();
SharedPreferences sharedPreferencesdx = getSharedPreferences(ARQUVO_MENSAGEMASS, 0);
SharedPreferences.Editor editdx = sharedPreferencesdx.edit();
if (sharedPreferencesdx.contains("INGLES")){
oi = "HELLO WORLD!!!!!!!!!!!";
}else{
oi = "OLÁ MUNDO!!!!!!!!!!!";
}
final NotificationManager mNM = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(), TelaPrincipal.class);
final int _conversa = (int) System.currentTimeMillis();
PendingIntent pIntent = PendingIntent.getActivity(this,_conversa, intent1, 0);
Notification mNotify = new Notification.Builder(this)
.setContentTitle(nomedoAssistente + ":")
.setContentText(oi)
.setSmallIcon(R.drawable.ciencia)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
}
@Override
public void onDestroy() {
super.onDestroy();
if (wakeLock.isHeld()) {
//Verificou-se que o iluminar do ecrã
//não acontecia devido ao WakeLock ser
//rapidamente libertado(apesar de PowerManager.ON_AFTER_RELEASE !?).
try {
//Atrasa a libertação do WakeLock
//de forma a permitir a iluminação do ecrâ.
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
wakeLock.release();
}
}
}
}
No Manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name=".Comofoiseudia.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".Comofoiseudia.Som"
android:enabled="true">
</service>