Yes I found the answer by myself.
There are 3 different your current activity status.
- Resumed - Static reference is available
- Stopped - Static reference is available
- Destroyed - Static references are garbaged
//Define static variable of your activity instance
public static FleetLocActivity mFleetLocActivity = null;
And write the below code in your GCMIntentService. This class is called whenever there is a new push message.
if (FleetLocActivity.mFleetLocActivity != null) {
**//Activity is in Stopped or Resumed State**
handlePushMessage(pushMessage);
// Start Service and Update UI with the help of Handler
} else {
**//Activity is in Destroyed State**
// Post notification of received message. And Add the action of opening your home activity. When the user clicks the notification it will open the home activity and start the respective service by using activity instance.
mAppUtilInstance.postGcmCommandNotification(
"Command Received : " + pushMessage, mContext);
}
//Class variable updated with the received push message
private String pushMessage = "";
private void handlePushMessage(String pushMsg){
Message msgObj = gcmCommandHandler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putString(Constants.getInstance().GCM_SERVER_MESSAGE,pushMessage);
msgObj.setData(bundle);
gcmCommandHandler.sendMessage(msgObj);
}
/**
* to handle the GCM push message (or) commands using Handler
*/
private Handler gcmCommandHandler = new Handler() {
// Create handleMessage function
public void handleMessage(Message message) {
String pushMessage = message.getData().getString(
Constants.getInstance().GCM_SERVER_MESSAGE);
if (pushMessage != null && pushMessage.length() > 0) {
//Start Service and update UI HERE
}
}
};