21 Kasım 2017 Salı

PendingIntent Sınıfı

Giriş
Bu sınıfı bir servisten başka bir uygulamaya notification göndermek için kullanabiliriz.

getBroadcast metodu
Örnek
Şöyle yaparız.
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
  PendingIntent.FLAG_CANCEL_CURRENT);
Örnek
Şöyle yaparız.
Intent intent = new Intent(this, NotificationBarButtonListener.class);
intent.setAction("ADD_BOOKMARK_ACTION");
PendingIntent pendingAddBookmarkIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Listener şöyledir.
public static class NotificationBarButtonListener extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {

    final String action = intent.getAction();
    if(action.equals("ADD_BOOKMARK_ACTION")){
      ...
    }
  }
}
Servis's şu satırı ekleriz.
<receiver android:name=".NotificationBarService$NotificationBarButtonListener"/>

Hiç yorum yok:

Yorum Gönder