从Xamarin Android中的BroadcastReceiver通知中调用服务方法
问题描述:
我启动一个服务并创建新线程(下载一个大文件)。与此同时,我会显示带有操作的通知(暂停按钮)。 当我按下这个按钮“WSTRZYMAJ”时,我想从我的服务中调用PauseDownload()方法。我该怎么做?我已阅读有关BroadcastReceiver,创建此类,但如何从BroadcastReceiver类的服务调用方法?从通知从Xamarin Android中的BroadcastReceiver通知中调用服务方法
屏幕:
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(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
}
}
答
如何从服务从广播接收器类调用方法?
当您收到DownloadsBroadcastReceiver
的消息时,您可以再次启动您的DownloadsService
。当你这样做时,由于你的DownloadsService
已经创建,所以会调用OnStartCommand()的方法,所以你会收到这个方法的消息,你可以拨打PauseDownload()
的OnStartCommand()
方法。
用法是这样的:
[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;
}
作品,谢谢:) –