Android Open Source - BBC-News-Reader Service Manager






From Project

Back to project page BBC-News-Reader.

License

The source code is released under:

Copyright (c) 2011, 2012, Digital Lizard (Oscar Key, Thomas Boby) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the...

If you think the Android project BBC-News-Reader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.digitallizard.bbcnewsreader;
//from  w  w w. j  av  a 2  s  . c  o  m
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.ListIterator;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

public class ServiceManager {
  
  private Context context;
  private Messenger resourceMessenger;
  private Messenger messenger;
  private boolean resourceServiceBound;
  private ArrayList<Message> messageQueue;
  
  public interface MessageReceiver {
    public void handleMessage(Message msg);
  }
  
  static class IncomingHandler extends Handler {
    private final WeakReference<MessageReceiver> receiver;    
    
    IncomingHandler(MessageReceiver receiver) {
      this.receiver = new WeakReference<MessageReceiver>(receiver);
    }
    
    @Override
    public void handleMessage(Message msg) {
      // pass the message on
      receiver.get().handleMessage(msg);
    }
  }
    
  private ServiceConnection resourceServiceConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
      resourceServiceBound = true; // flag the service as bound
      resourceMessenger = new Messenger(service); // allows us to talk to the service
      // try and tell the service that we have connected so it will keep talking to us
      sendMessageToService(ResourceService.MSG_REGISTER_CLIENT);
      
      // send any queued messages
      ListIterator<Message> iterator = messageQueue.listIterator();
      while (iterator.hasNext()) {
        sendMessageToService(iterator.next());
      }
      messageQueue.clear();
    }
    
    public void onServiceDisconnected(ComponentName className) {
      // this runs if the service randomly disconnects if this happens there are more problems than a missing service
      resourceMessenger = null; // as the service no longer exists, destroy its pointer
    }
  };
  
  public void doBindService() {
    // bind the service
    context.bindService(new Intent(context, ResourceService.class), resourceServiceConnection, Context.BIND_AUTO_CREATE);
    resourceServiceBound = true;
  }
  
  public void doUnbindService() {
    // disconnect the resource service
    // check if the service is bound, if so, disconnect it
    if (resourceServiceBound) {
      // politely tell the service that we are disconnected
      sendMessageToService(ResourceService.MSG_UNREGISTER_CLIENT);
      // remove local references to the service
      context.unbindService(resourceServiceConnection);
      resourceServiceBound = false;
    }
  }
  
  public void sendMessageToService(Message msg) {
    // check the service is bound before trying to send a message
    if (resourceServiceBound && resourceMessenger != null) {
      try {
        resourceMessenger.send(msg); // send the message
      } catch (RemoteException e) {
        // we are probably shutting down, but report it anyway
        Log.e("ERROR", "Unable to send message to service: " + e.getMessage());
      }
    }
    else {
      // things haven't initialized yet, queue the message up for sending in a bit
      messageQueue.add(msg);
    }
  }
  
  public void sendMessageToService(int what, Bundle bundle) {
    // create a message according to parameters
    Message msg = Message.obtain(null, what);
    // add the bundle if needed
    if (bundle != null) {
      msg.setData(bundle);
    }
    msg.replyTo = messenger; // tell the service to reply to us, if needed
    
    // check the service is bound before trying to send a message
    if (resourceServiceBound && resourceMessenger != null) {
      try {
        resourceMessenger.send(msg); // send the message
      } catch (RemoteException e) {
        // we are probably shutting down, but report it anyway
        Log.e("ERROR", "Unable to send message to service: " + e.getMessage());
      }
    }
    else {
      // things haven't initialized yet, queue the message up for sending in a bit
      messageQueue.add(msg);
    }
  }
  
  public void sendMessageToService(int what) {
    sendMessageToService(what, null);
  }
  
  public ServiceManager(Context context, MessageReceiver receiver) {
    this.context = context;
    
    messenger = new Messenger(new IncomingHandler(receiver));
    messageQueue = new ArrayList<Message>();
  }
}




Java Source Code List

com.digitallizard.bbcnewsreader.ArticleActivity.java
com.digitallizard.bbcnewsreader.CategoryActivity.java
com.digitallizard.bbcnewsreader.CategoryChooserActivity.java
com.digitallizard.bbcnewsreader.CategoryChooserAdapter.java
com.digitallizard.bbcnewsreader.CategoryPagerAdapter.java
com.digitallizard.bbcnewsreader.Eula.java
com.digitallizard.bbcnewsreader.ItemAdapter.java
com.digitallizard.bbcnewsreader.ItemLayout.java
com.digitallizard.bbcnewsreader.Item.java
com.digitallizard.bbcnewsreader.RSSManager.java
com.digitallizard.bbcnewsreader.ReaderActivity.java
com.digitallizard.bbcnewsreader.ResourceInterface.java
com.digitallizard.bbcnewsreader.ResourceService.java
com.digitallizard.bbcnewsreader.ServiceManager.java
com.digitallizard.bbcnewsreader.SettingsActivity.java
com.digitallizard.bbcnewsreader.data.DatabaseHandler.java
com.digitallizard.bbcnewsreader.data.DatabaseHelper.java
com.digitallizard.bbcnewsreader.data.DatabaseProvider.java
com.digitallizard.bbcnewsreader.data.ItemClearer.java
com.digitallizard.bbcnewsreader.data.WrapBackwards.java
com.digitallizard.bbcnewsreader.fragments.ArticleFragment.java
com.digitallizard.bbcnewsreader.fragments.CategoryChooserFragment.java
com.digitallizard.bbcnewsreader.fragments.CategoryFragment.java
com.digitallizard.bbcnewsreader.fragments.FrontpageFragment.java
com.digitallizard.bbcnewsreader.resource.web.HtmlParser.java
com.digitallizard.bbcnewsreader.resource.web.ImageDownloader.java
com.digitallizard.bbcnewsreader.resource.web.QueueItem.java
com.digitallizard.bbcnewsreader.resource.web.WebManager.java
com.digitallizard.bbcnewsreader.widget.ReaderWidget.java
com.digitallizard.bbcnewsreader.widget.WidgetConfigActivity.java
com.hlidskialf.android.preference.SeekBarPreference.java