I have a problem with my Alarm, I wan't to display a Notification every day, so I have tried these code:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 00);
Intent downloader = new Intent(getApplicationContext(), NotificationDemandeReceiver.class);
final PendingIntent recurringDownload = PendingIntent.getBroadcast(this, 0,
downloader, 0);
AlarmManager alarms = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringDownload);
AND
Intent myIntent = new Intent(this , NotificationDemandeService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);
The problem with these two codes, is: if i schedule to 19:00 and if the application is not running at this time, the notification will nether be show.
And if the application is running at scheduled time, the notification will be show but after if I close the application and run again the notification will be show again !
So I did this:
// Construct an intent that will execute the AlarmReceiver
Intent nintent = new Intent(getApplicationContext(), NotificationDemandeReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent npIntent = PendingIntent.getBroadcast(this, NotificationDemandeReceiver.REQUEST_CODE,
nintent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every 5 seconds
long nfirstMillis = System.currentTimeMillis(); // first run of alarm is immediate
int nintervalMillis = 1000 * 60 * 60 * 24; // 10 minutes
AlarmManager nalarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
nalarm.setRepeating(AlarmManager.RTC_WAKEUP, nfirstMillis, nintervalMillis, npIntent);
- Here to, the Alarm run when i start the application but if the application is running at scheduled time, the notification will be show but after if I close the application and run again the notification will be show again !
The Alarme are in a function public void scheduleAlarm() {} And I call the on my Splashscreen:
Thread logoTimer = new Thread() {
public void run() {
try {
int logoTimer = 0;
while (logoTimer < 10000) {
sleep(100);
logoTimer = logoTimer + 1000;
};
List<Demande> demandesRenvoie = db.getAllDemandesRenvoie();
if (!(demandesRenvoie.isEmpty())) {
scheduleAlarm();
}
startActivity(new Intent("android.intent.action.ACCUEIL"));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
finish();
}
}
};
So how to have a Alarm scheduled every day, and with no "restart" after closing an running the application ?
UPDATE (Working on API 14)
Splashscreen (I start 2 services, one for ksoapservice (every 10 min) and the another for notification (every day)):
public class Splashscreen extends Activity {
DatabaseHandler db = new DatabaseHandler(this);
//Initialisation de l activite avec les donnees necessaires
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
Thread logoTimer = new Thread() {
public void run() {
try {
int logoTimer = 0;
while (logoTimer < 10000) {
sleep(100);
logoTimer = logoTimer + 1000;
};
List<Demande> demandesRenvoie = db.getAllDemandesRenvoie();
if (!(demandesRenvoie.isEmpty())) {
scheduleAlarm();
}
startActivity(new Intent("android.intent.action.ACCUEIL"));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
finish();
}
}
};
logoTimer.start();
}
public void scheduleAlarm() {
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(getApplicationContext(), EnvoieReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(this, EnvoieReceiver.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every 5 seconds
long firstMillis = System.currentTimeMillis(); // first run of alarm is immediate
int intervalMillis = 1000 * 60 * 10; // 10 minutes
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, intervalMillis, pIntent);
// Construct an intent that will execute the AlarmReceiver
Intent nintent = new Intent(getApplicationContext(), NotificationDemandeReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent npIntent = PendingIntent.getBroadcast(this, NotificationDemandeReceiver.REQUEST_CODE,
nintent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every 5 seconds
long nfirstMillis = System.currentTimeMillis(); // first run of alarm is immediate
int nintervalMillis = 1000 * 60 * 60 * 24; // 10 minutes
AlarmManager nalarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
nalarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, nfirstMillis, nintervalMillis, npIntent);
/*
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 00);
Intent downloader = new Intent(getApplicationContext(), NotificationDemandeReceiver.class);
final PendingIntent recurringDownload = PendingIntent.getBroadcast(this, 0,
downloader, 0);
AlarmManager alarms = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringDownload);
*/
/*
Intent myIntent = new Intent(this , NotificationDemandeService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 19);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);
*/
}
Receiver:
public class NotificationDemandeReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
public static final String ACTION = "fr.solutis.solutis.alarm";
// Triggered by the Alarm periodically (starts the service to run task)
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, NotificationDemandeService.class);
i.putExtra("foo", "bar");
context.startService(i);
}
}
Service:
public class NotificationDemandeService extends IntentService {
DatabaseHandler db = new DatabaseHandler(this);
public NotificationDemandeService() {
super("EnvoieService");
}
@Override
protected void onHandleIntent(Intent intent) {
List<Demande> demandes = db.getAllDemandesRenvoie();
if (!(demandes.isEmpty())) {
String message = "Votre demande n'a pas été envoyée, merci de vous connecter à internet.";
int icon = R.drawable.notification_icon;
int mNotificationId = 001;
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
Notification notification = mBuilder.setSmallIcon(icon).setTicker("Solutis").setWhen(0)
.setAutoCancel(true)
.setContentTitle("Solutis")
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(resultPendingIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher_solutis))
.setContentText(message).build();
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mNotificationId, notification);
}
}
}
Android Manifest:
...
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- Optional permission for reliable local dispatching on non-Google Play devices -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
...
<receiver android:name=".EnvoieReceiver" android:process=":remote" >
</receiver>
<receiver android:name=".NotificationDemandeReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".EnvoieService" android:exported="false" />
<service android:name=".NotificationDemandeService" android:exported="false" />