简体   繁体   中英

Pass data from a service to current activity

I'm building a chat application that uses GCM to push the messages and the notifications.

In the app, I have an activity which lists the conversation threads ( ConversationListActivity ) and an activity which shows the conversation inside the thread ( ConversationActivity ).

I capture new messages in GCMIntentService's onMessage method. The notification works and the message is recorded as expected.

public class GCMIntentService extends GCMBaseIntentService {
  @Override
  protected void onMessage(Context context, Intent intent) {
    Bundle extras = intent.getExtras();
    String sender = extras.getString("sender");
    String message = extras.getString("message");

    ConversationModel model = new ConversationModel(context);
    model.open();
    model.createMessage(sender, message.trim(), true, true, ConversationMessage.STATUS_SENT);
    model.close();

    this.createMessageNotification(context, sender, message);
  }
}

The problem is, if GCM pushes a new message while ConversationActivity is active, I don't want it to generate a notification. Instead, I want the conversation to refresh directly.

How can I tell the active ConversationActivity to refresh the message list every time there is a new message from GCM?

I found the solution myself. I'm sure that this is not the best way to do it, but at least it works for me.

Create a singleton class that derives Handler to store the active ConversationActivity . In the handleMessage method, handle message that will be dispatched from the service that tells the handler to tell the activity to refresh the conversation.

public class ActiveMessageHandler extends Handler {

  private static ActiveMessageHandler _instance = new ActiveMessageHandler();
  private Activity _activity = null;

  @Override
  public void handleMessage(Message message) {
    if(message.obj.toString().equals("gcmNewMessage") && _activity != null)
        ((ConversationActivity)_activity).repopulateList();

    super.handleMessage(message);
  }

  public static ActiveMessageHandler instance() {
    return _instance;
  }

  public void setActivity(Activity activity) {
    _activity = activity;
  }

  public Activity getActivity() {
    return _activity;
  }

}

Then, in ConversationActivity , set the activity attribute in the handler.

public class ConversationActivity extends Activity {
  @Override
  public void onStart() {
    super.onStart();
    ActiveMessageHandler.instance().setActivity(this);
  }

  @Override
  public void onStop() {
    super.onStop();
    ActiveMessageHandler.instance().setActivity(null);
  }
}

Finally, in the service, check if the activity in the handler is not null and send a message to the handler to refresh the conversation.

public class GCMIntentService extends GCMBaseIntentService {
    @Override
    protected void onMessage(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        String sender = extras.getString("sender");
        String message = extras.getString("message");

        ConversationModel model = new ConversationModel(context);
        model.open();
        model.createMessage(sender, message.trim(), true, true, ConversationMessage.STATUS_SENT);
        model.close();

        // if ConversationActivity is active, send a message to handler to refresh the conversation
        if(ActiveMessageHandler.instance().getActivity() != null)
        {
            Message msg = Message.obtain(ActiveMessageHandler.instance());
            msg.obj = "gcmNewMessage";
            ActiveMessageHandler.instance().sendMessage(msg);

            // Optionally, play a notification sound
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
            r.play();
        }
        else
            createMessageNotification(context, sender, message);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM