// 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"); }
/// <summary> /// Schedule the download of the big picture associated with the content. </summary> /// <param name="context"> any application context. </param> /// <param name="content"> content with big picture notification. </param> public static void downloadBigPicture(Context context, EngagementReachInteractiveContent content) { /* Set up download request */ DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE); Uri uri = Uri.parse(content.NotificationBigPicture); DownloadManager.Request request = new DownloadManager.Request(uri); request.NotificationVisibility = DownloadManager.Request.VISIBILITY_HIDDEN; request.VisibleInDownloadsUi = false; /* Create intermediate directories */ File dir = context.getExternalFilesDir("engagement"); dir = new File(dir, "big-picture"); dir.mkdirs(); /* Set destination */ long contentId = content.LocalId; request.DestinationUri = Uri.fromFile(new File(dir, contentId.ToString())); /* Submit download */ long id = downloadManager.enqueue(request); content.setDownloadId(context, id); /* Set up timeout on download */ Intent intent = new Intent(EngagementReachAgent.INTENT_ACTION_DOWNLOAD_TIMEOUT); intent.putExtra(EngagementReachAgent.INTENT_EXTRA_CONTENT_ID, contentId); intent.Package = context.PackageName; PendingIntent operation = PendingIntent.getBroadcast(context, (int)contentId, intent, 0); long triggerAtMillis = SystemClock.elapsedRealtime() + DOWNLOAD_TIMEOUT; AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); alarmManager.set(ELAPSED_REALTIME_WAKEUP, triggerAtMillis, operation); }
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); } }