0

Je démarre un service et crée un nouveau thread (téléchargement d'un fichier volumineux). En même temps, j'affiche une notification avec action (bouton pause). Quand j'appuie sur ce bouton "WSTRZYMAJ" je veux appeler la méthode PauseDownload() de mon service. Comment puis-je faire cela? J'ai lu à propos de BroadcastReceiver, créer cette classe, mais comment appeler la méthode du service de la classe BroadcastReceiver?Méthode de service d'appel à partir de la notification avec BroadcastReceiver dans Xamarin Android

écran de notification: NOTIFICATION

Fragment de mon service:

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

et classe complète code BroadcastReceiver:

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

Répondre

1

comment appeler la méthode du service de la classe BroadcastReceiver ?

Lorsque vous recevez un message dans DownloadsBroadcastReceiver, vous pouvez commencer votre DownloadsService à nouveau. Lorsque vous avez fait cela, puisque votre DownloadsService a été créé, il va appeler la méthode OnStartCommand(), donc vous recevrez un message dans cette méthode, vous pouvez appeler PauseDownload() en OnStartCommand() méthode.

Utilisation comme ceci:

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

Dans votre DownloadsService classe:

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

    ... 
    return StartCommandResult.NotSticky; 
} 
+0

Works, merci :) –