Xamarin Android中具有BroadcastReceiver的通知中的呼叫服务方法

编程入门 行业动态 更新时间:2024-10-10 19:22:16
本文介绍了Xamarin Android中具有BroadcastReceiver的通知中的呼叫服务方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我启动服务并创建新线程(下载大文件).同时,我显示一个带有操作的通知(暂停按钮). 当我按下"WSTRZYMAJ"按钮时,我想从服务中调用PauseDownload()方法.我该怎么做?我已经阅读了BroadcastReceiver,创建了这个类,但是如何从BroadcastReceiver类的服务中调用方法呢?

I start a service and create new thread (downloading a large file). At the same time I display a notification with action (pause button). When I press this button "WSTRZYMAJ" I want call PauseDownload() method from my service. How can I do this? I have read about BroadcastReceiver, create this class but how to call method from service from BroadcastReceiver class?

通知屏幕:

我的服务片段:

class DownloadsService : Service { DownloadsBroadcastReceiver receiver; Notification.Builder notificationBuilder; DownloadsData downloadsData; int uniqueNumber = 1000; bool isStarted; public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { RegisterReceiver(receiver, new IntentFilter("com.xamarin.example.TEST")); downloadsData = JsonConvert.DeserializeObject<DownloadsData>(intent.GetStringExtra("downloadsData")); if (isStarted) Log.Info("DAMIAN", "Error start service"); else { Log.Info("DAMIAN", "Start service"); DispatchNotificationThatServiceIsRunning(downloadsData.Name, "Started"); new Thread(new ThreadStart(() => { MakeDownload(); })).Start(); isStarted = true; } return StartCommandResult.NotSticky; } private void DispatchNotificationThatServiceIsRunning(string title, string content) { Intent stopIntent = new Intent(this, typeof(DownloadsBroadcastReceiver)); stopIntent.PutExtra("action", "actionName"); PendingIntent stopPi = PendingIntent.GetBroadcast(this, 4, stopIntent, PendingIntentFlags.UpdateCurrent); Intent intent = new Intent(this, typeof(MainActivity)); TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this); stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity))); stackBuilder.AddNextIntent(intent); PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent); Notification.Action pauseAction = new Notification.Action.Builder(Resource.Drawable.Pause, "WSTRZYMAJ", stopPi).Build(); notificationBuilder = new Notification.Builder(this) .SetSmallIcon(Resource.Drawable.Icon) .SetContentIntent(resultPendingIntent) .SetContentTitle(title) .SetContentText(content) .AddAction(pauseAction); var notificationManager = (NotificationManager)GetSystemService(NotificationService); notificationManager.Notify(uniqueNumber, notificationBuilder.Build()); } private void UpdateNotification(string content) { notificationBuilder.SetContentText(content); var notificationManager = (NotificationManager)GetSystemService(NotificationService); notificationManager.Notify(uniqueNumber, notificationBuilder.Build()); } private void MakeDownload() { //downloading file } private void PauseDownload() { //pause downloading } }

以及完整的BroadcastReceiver类代码:

And full code BroadcastReceiver class:

[BroadcastReceiver(Enabled = true, Exported = false)] [IntentFilter(new[] { "com.xamarin.example.TEST" })] class DownloadsBroadcastReceiver : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { String action = intent.GetStringExtra("action"); if (action.Equals("actionName")) Log.Info("DAMIAN", "BROADCAST"); //it works, ie. I have text "BROADCAST" in log } }

推荐答案

如何从BroadcastReceiver类的服务中调用方法?

how to call method from service from BroadcastReceiver class?

当您在DownloadsBroadcastReceiver中收到消息时,可以再次启动DownloadsService.完成此操作后,由于已创建DownloadsService,它将调用 OnStartCommand ()方法,因此您将在此方法中收到消息,您可以在OnStartCommand()方法中调用PauseDownload().

When you receive message in DownloadsBroadcastReceiver, you could start your DownloadsService again. When you did this, since your DownloadsService has been created, it will call OnStartCommand() method, so you will receive message in this method, you could call PauseDownload() in OnStartCommand() method.

这样的用法:

[BroadcastReceiver(Enabled = true, Exported = false)] [IntentFilter(new[] { "com.xamarin.example.TEST" })] public class DownloadsBroadcastReceiver : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { String action = intent.GetStringExtra("action"); if ("actionName".Equals(action)) { Intent intent2 = new Intent(Application.Context, typeof(DownloadsService)); intent2.PutExtra(action, "actionName"); Application.Context.StartService(intent2); } } }

在您的DownloadsService类别中:

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { String action = intent.GetStringExtra("action"); if ("actionName".Equals(action)) { PauseDownload(); } ... return StartCommandResult.NotSticky; }

更多推荐

Xamarin Android中具有BroadcastReceiver的通知中的呼叫服务方法

本文发布于:2023-11-27 10:59:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   方法   Xamarin   Android   BroadcastReceiver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!