0

我啓動一個服務並創建新線程(下載一個大文件)。與此同時,我會顯示帶有操作的通知(暫停按鈕)。 當我按下這個按鈕「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 
    } 
} 

和全碼廣播接收器類: NOTIFICATION

我服務的片段

[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 
    } 
} 

回答

1

如何從服務從廣播接收器類調用方法?

當您收到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; 
} 
+0

作品,謝謝:) –