예제 #1
0
        private void ShowNotification(bool setPlayImage)
        {
            var mainActivity = (MainActivity)CrossCurrentActivity.Current.Activity;

            NotificationCompat.Builder builder = new NotificationCompat.Builder(mainActivity)
                                                 .SetOngoing(true)
                                                 .SetSmallIcon(Resource.Drawable.ic_headset_white);

            var contentView = new RemoteViews(mainActivity.PackageName, Resource.Layout.audioNotificationView);

            contentView.SetOnClickPendingIntent(Resource.Id.btnPlayPause, GetIntentForAction(PlayPauseAction));
            contentView.SetOnClickPendingIntent(Resource.Id.btnStop, GetIntentForAction(StopAction));

            contentView.SetTextViewText(Resource.Id.textViewTitle, AudioTitle);
            contentView.SetImageViewResource(Resource.Id.btnPlayPause, setPlayImage ? Resource.Drawable.ic_pause : Resource.Drawable.ic_play_arrow);
            contentView.SetProgressBar(Resource.Id.audio_progress_bar, mediaPlayer.Duration, mediaPlayer.CurrentPosition, false);

            builder = builder.SetCustomContentView(contentView);

            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            {
                builder.SetVisibility((int)NotificationVisibility.Public);
            }

            // Finally, publish the notification
            var notificationManager = (NotificationManager)mainActivity.GetSystemService(Context.NotificationService);

            //notificationManager.Notify(AudioPlayerNotificationId, notificationBuilder.Build ());
            notificationManager.Notify(AudioPlayerNotificationId, builder.Build());
        }
예제 #2
0
        /// <summary>
        /// Update the notification.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// The updated notification.
        /// </returns>
        public Notification UpdateNotification(Context context)
        {
            bool hasPausedText = this.PausedText != null;
            bool mustUpdate    = this.lastHasPausedText != hasPausedText;

            // Build the RemoteView object
            if (expandedView == null)
            {
                expandedView = new RemoteViews(context.PackageName, Resource.Layout.status_bar_ongoing_event_progress_bar);
                expandedView.SetImageViewResource(Resource.Id.appIcon, this.Icon);
                expandedView.SetTextViewText(Resource.Id.paused_text, this.PausedText);
                expandedView.SetTextViewText(Resource.Id.title, this.Title);

                this.notification.ContentView = expandedView;

                mustUpdate = true;
            }

            if (mustUpdate)
            {
                expandedView.SetViewVisibility(Resource.Id.progress_bar_frame, hasPausedText ? ViewStates.Gone : ViewStates.Visible);
                expandedView.SetViewVisibility(Resource.Id.description, hasPausedText ? ViewStates.Gone : ViewStates.Visible);
                expandedView.SetViewVisibility(Resource.Id.time_remaining, hasPausedText ? ViewStates.Gone : ViewStates.Visible);
                expandedView.SetViewVisibility(Resource.Id.paused_text, hasPausedText ? ViewStates.Visible : ViewStates.Gone);
            }

            if (!hasPausedText)
            {
                expandedView.SetTextViewText(Resource.Id.progress_text, Helpers.GetDownloadProgressPercent(this.CurrentBytes, this.TotalBytes));
                expandedView.SetTextViewText(Resource.Id.description, Helpers.GetDownloadProgressString(this.CurrentBytes, this.TotalBytes));
                expandedView.SetProgressBar(
                    Resource.Id.progress_bar,
                    (int)(this.TotalBytes >> 8),
                    (int)(this.CurrentBytes >> 8),
                    this.TotalBytes <= 0);
                expandedView.SetTextViewText(Resource.Id.time_remaining, string.Format("Time remaining: {0}", Helpers.GetTimeRemaining(this.TimeRemaining)));
            }

            this.lastHasPausedText = hasPausedText;

            return(this.notification);
        }
예제 #3
0
        public void Notify(int rate)
        {
            try
            {
                contentView.SetProgressBar(Resource.Id.progress, 100, rate, false);
                contentView.SetTextViewText(Resource.Id.notificationTitle, "正在下载更新");
                contentView.SetTextViewText(Resource.Id.notificationPercent, rate + "%");

                notification.ContentView = contentView;
                notification.Flags       = NotificationFlags.ForegroundService;
                notification.Icon        = Resource.Drawable.icon;
                p = PendingIntent.GetActivity(Application.Context.ApplicationContext, 0, new Intent(Intent.ActionView), 0);
                notification.ContentIntent = p;

                // Publish the notification:
                const int notificationId = 0;
                notificationManager.Notify(notificationId, notification);
            }
            catch (Exception)
            {
            }
        }
예제 #4
0
        /// <summary>
        /// Update the notification.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// The updated notification.
        /// </returns>
        public Notification UpdateNotification(Context context)
        {
            // Build the RemoteView object
            if (expandedView == null)
            {
                expandedView = new RemoteViews(context.PackageName, Resource.Layout.status_bar_ongoing_event_progress_bar);
            }

            expandedView.SetImageViewResource(Resource.Id.appIcon, this.Icon);
            expandedView.SetTextViewText(Resource.Id.title, this.Title);
            expandedView.SetTextViewText(Resource.Id.progress_text, Helpers.GetDownloadProgressPercent(this.CurrentBytes, this.TotalBytes));
            expandedView.SetTextViewText(Resource.Id.description, Helpers.GetDownloadProgressString(this.CurrentBytes, this.TotalBytes));
            expandedView.SetProgressBar(
                Resource.Id.progress_bar,
                (int)(this.TotalBytes >> 8),
                (int)(this.CurrentBytes >> 8),
                this.TotalBytes <= 0);
            expandedView.SetTextViewText(Resource.Id.time_remaining, context.GetString(Resource.String.time_remaining_notification, Helpers.GetTimeRemaining(this.TimeRemaining)));

            this.notification.ContentView = expandedView;

            return(this.notification);
        }