public virtual Notification getHiddenNotification(int id) { // same basics as getPersistentNotification() int icon = getHiddenIcon(); long when = java.lang.System.currentTimeMillis(); Context c = getApplicationContext(); string contentTitle = getHiddenNotificationTitle(id); string contentText = getHiddenNotificationMessage(id); string tickerText = contentTitle + ": " + contentText; // the difference here is we are providing the same id Intent notificationIntent = getHiddenNotificationIntent(id); PendingIntent contentIntent = null; if (notificationIntent != null) { contentIntent = PendingIntent.getService(this, 0, notificationIntent, // flag updates existing persistent notification PendingIntent.FLAG_UPDATE_CURRENT); } Notification notification = new Notification(icon, tickerText, when); notification.setLatestEventInfo(c, contentTitle, contentText, contentIntent); return(notification); }
public static void CancelPendingAlarm(this Context that, Class IntentClass) { // http://stackoverflow.com/questions/6522792/get-list-of-active-pendingintents-in-alarmmanager var myIntent = new Intent(that, IntentClass); var pendingIntent = PendingIntent.getService(that, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager)that.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(pendingIntent); }
// http://stackoverflow.com/questions/6274141/trigger-background-service-at-a-specific-time-in-android // http://stackoverflow.com/questions/7144908/how-is-an-intent-service-declared-in-the-android-manifest // http://developer.android.com/guide/topics/manifest/service-element.html protected override void onCreate(global::android.os.Bundle savedInstanceState) { base.onCreate(savedInstanceState); ScrollView sv = new ScrollView(this); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); sv.addView(ll); #region startservice var startservice = new Button(this).WithText("Start Service to send Notification").AtClick( delegate { this.ShowToast("start"); var myIntent = new Intent(this, typeof(MyAlarmService).ToClass()); this.pendingIntent = PendingIntent.getService(this, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager)this.getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, 1000 * 5, this.pendingIntent); } ); ll.addView(startservice); #endregion #region stopservice var stopservice = new Button(this).WithText("Stop Service").AtClick( delegate { this.ShowToast("stop"); AlarmManager alarmManager = (AlarmManager)this.getSystemService(ALARM_SERVICE); alarmManager.cancel(this.pendingIntent); } ); ll.addView(stopservice); #endregion this.setContentView(sv); //this.ShowToast("http://jsc-solutions.net"); }
public virtual PersistentNotification getPersistentNotification(int id) { // basic notification stuff // http://developer.android.com/guide/topics/ui/notifiers/notifications.html int icon = getAppIcon(); long when = java.lang.System.currentTimeMillis(); Context c = getApplicationContext(); string contentTitle = getPersistentNotificationTitle(id); string contentText = getPersistentNotificationMessage(id); string tickerText = contentTitle + ": " + contentText; // getPersistentNotification() is called for every new window // so we replace the old notification with a new one that has // a bigger id Intent notificationIntent = getPersistentNotificationIntent(id); PendingIntent contentIntent = null; if (notificationIntent != null) { contentIntent = PendingIntent.getService(this, 0, notificationIntent, // flag updates existing persistent notification PendingIntent.FLAG_UPDATE_CURRENT); } Notification notification = new Notification(icon, tickerText, when); notification.setLatestEventInfo(c, contentTitle, contentText, contentIntent); var nn = new PersistentNotification { Notification = notification, id = id, contentIntent = contentIntent, contentText = contentText, contentTitle = contentTitle, context = c }; PersistentNotifications.Add(nn); return(nn); }
public static void StartPendingAlarm(this Context that, Class IntentClass, long delay = 1000 * 5, long repeat = 1000 * 25) { that.CancelPendingAlarm(IntentClass); var myIntent = new Intent(that, IntentClass); var pendingIntent = PendingIntent.getService(that, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager)that.getSystemService(Context.ALARM_SERVICE); if (repeat > 0) { alarmManager.setInexactRepeating( AlarmManager.RTC, delay, repeat, pendingIntent ); } else { alarmManager.set(AlarmManager.RTC, delay, pendingIntent); } }