studio sgoliver segundo programacion plano notificaciones notificacion example ejemplo basica android notifications broadcastreceiver

sgoliver - notificationmanager android ejemplo



Las notificaciones programadas activan cada vez que abro la aplicaciĆ³n (3)

Estoy construyendo una aplicación que desencadena una notificación a las 11:30 IST. He probado que funciona, pero después de las 11:30. Cada vez que abro mi aplicación, emite una notificación ...

No sé por qué creo que debería haber algún error de condición en el control del tiempo ...

Quiero que la notificación se active a tiempo solo una vez al día ... y luego al día siguiente ... :)

por favor resuelve esto gracias de antemano

**MainActivity.java** @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = (ProgressBar)findViewById(R.id.progressBar); spinner.setVisibility(View.GONE); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 11); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 00); Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent1, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); } **AlarmReciever.java** package com.alivestats.alivestats; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.media.RingtoneManager; import android.net.Uri; import android.support.v4.app.NotificationCompat; public class AlarmReciever extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub int MID = 1; long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder( context).setSmallIcon(R.drawable.background) .setContentTitle("title") .setContentText("description").setSound(alarmSound) .setAutoCancel(true).setWhen(when) .setContentIntent(pendingIntent) .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}); notificationManager.notify(MID, mNotifyBuilder.build()); MID++; } }


Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class); intent1 .setAction("ilove"); public void onReceive(Context context, Intent intent) { if(!"ilove" .equalsIgnoreCase(getAction()))log.e("test",intent.getAction()); } <receiver android:name="com.android.app.AlaramReceiver" android:exported="false"> <intent-filter> <action android:name="ilove" /> </intent-filter> </receiver>


Después de tanto té y café codifiqué la respuesta yo mismo ...

long t = System.currentTimeMillis(); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 11); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 00); if (t <= calendar.getTimeInMillis()) { //calendar.add(Calendar.DATE, 6); //add 6 days. Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class); intent1.setAction("NOTE"); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); //am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); } else{ }

Acabo de comparar el tiempo del sistema con el tiempo de notificación ... si el tiempo de notificación es menor que el tiempo del sistema, es verdadero y el receptor funcionará ... pero si no, no pasará nada ...

Por cierto, gracias por ayudar @tiny y @Mahesh


Calendar calendar = Calendar.getInstance ();

calendar.set(Calendar.HOUR, 6); int myPreferedTime=calendar.get(Calendar.HOUR); Calendar rightNow = Calendar.getInstance(); int currentHour = rightNow.get(Calendar.HOUR_OF_DAY); Log.i("current time","is"+currentHour); int difference=myPreferedTime-currentHour; Log.i("Differencevalue","is"+difference); switch(difference) { case 1: rightNow.add(Calendar.HOUR_OF_DAY, 1); break; case 2: rightNow.add(Calendar.HOUR_OF_DAY, 2); break; case 3: rightNow.add(Calendar.HOUR_OF_DAY, 3); break; case 4: rightNow.add(Calendar.HOUR_OF_DAY, 4); break; case 5: rightNow.add(Calendar.HOUR_OF_DAY, 5); break; case 6: rightNow.add(Calendar.HOUR_OF_DAY, 0); break; case -1: rightNow.add(Calendar.HOUR_OF_DAY,23); break; case -2: rightNow.add(Calendar.HOUR_OF_DAY,22); break; case -3: rightNow.add(Calendar.HOUR_OF_DAY,21); break; case -4: rightNow.add(Calendar.HOUR_OF_DAY,20); break; case -5: rightNow.add(Calendar.HOUR_OF_DAY,19); break; case -6: rightNow.add(Calendar.HOUR_OF_DAY,18); break; case -7: rightNow.add(Calendar.HOUR_OF_DAY,17); break; case -8: rightNow.add(Calendar.HOUR_OF_DAY,16); break; case -9: rightNow.add(Calendar.HOUR_OF_DAY,15); break; case -10: rightNow.add(Calendar.HOUR_OF_DAY,14); break; case -11: rightNow.add(Calendar.HOUR_OF_DAY,13); break; case -12: rightNow.add(Calendar.HOUR_OF_DAY,12); break; case -13: rightNow.add(Calendar.HOUR_OF_DAY,11); break; case -14: rightNow.add(Calendar.HOUR_OF_DAY,10); break; case -15: rightNow.add(Calendar.HOUR_OF_DAY,9); break; case -16: Log.i("am here","bcoz time is 11 clock"); rightNow.add(Calendar.HOUR_OF_DAY, 8); break; case -17: rightNow.add(Calendar.HOUR_OF_DAY,7); break; case -18: rightNow.add(Calendar.HOUR_OF_DAY,6); break; default: break; } Intent intent1 = new Intent(MainActivity.this, MyReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, 0); Log.i("am sending", "yes"); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); Log.i("time holding with me", "calendar stores time like" + rightNow.getTimeInMillis()); am.setRepeating(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);