2

i have an homeActivity which contains a webview with an actionbar, the title of the actionbar is an textview

public static TextView mTitleTextView;

and also has an class which do receive gcm message

public class GCMNotificationIntentService extends IntentService {

after the app received a message i want to put the string to the textview of homeActivity, i tried to use

HomeActivity.mTitleTextView.setText("9999999999999999999999999999999999999999999999999999999999");

but the app shutdown with error, i've read some old post and googled see something like broadcast receiver can solve this problem, but i not really understand how it works, can anyone show some actually source code which can be applied in my situation?

hkguile
  • 4,235
  • 17
  • 68
  • 139

2 Answers2

3

We can achieve by using handler,broadcat and Listener concept.But I think broadcast is easy to implement and understand but need to take care or register and unregister of broadcast.

USING LISTENER

Create a Listener class

public Interface Listener{
public void onResultReceived(String str);
}

Now implement it in activity like below

public class MainActivity extends Activity implements listener{
public void onResultReceived(String str){
mTitleTextView.setText(str)
 }
}

Initialize your Listener by calling constructor of service from oncreate of Activity

new  GCMNotificationIntentService (MainActivity.this);

Now create public constructor of your service like below

public class GCMNotificationIntentService extends IntentService {
public static Listener listener_obj;
public GCMNotificationIntentService (Listener listener)
{
listener_obj=listener;
}

Listener.onResultReceived("99999999999999999999999999999999999999999");
//send the data which should be shown on textview

USING BROADCAST

 registerReceiver( mMessageReceiver, new IntentFilter("GETDATA"));
 //register localbraodcast with receiver object and intent filter inside oncreate

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

         String str= intent.getStringExtra("DATA", "No Data");
         mTitleTextView.setText(str);
}

};
ondestroy()
{

 unregisterReceiver( mMessageReceiver);
}

Sending Data from service

Intent intent = new Intent("GETDATA");
intent.putExtra("DATA", "9999999");
sendBroadcast(intent)
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
  • my IntentService class already has public GCMNotificationIntentService() { super("GcmIntentService"); }, i alter it into public GCMNotificationIntentService(Listener listener) { super("GcmIntentService"); listener_obj=listener;} , the app shutdown immediately when start – hkguile Jun 23 '15 at 07:48
  • don't do that create another separate constructor and add it to service.Purpose of this constructor is only to initilaize listener nothing else – Shakeeb Ayaz Jun 23 '15 at 08:06
  • So, how can i initialize listener without using constructor? – hkguile Jun 23 '15 at 08:10
  • but GCMNotificationIntentService not allow me to add public IntentService (Listener listener) { listener_obj=listener; } – hkguile Jun 23 '15 at 08:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81244/discussion-between-shakeeb-ayaz-and-hkguile). – Shakeeb Ayaz Jun 23 '15 at 08:35
0

Use a handler and send a message to parent activity from the Intentservice

Parent Activity :

Declaring Handler

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
            Bundle reply = msg.getData();
                // do whatever with the bundle here
            }
};

Invoking the intentservice:

    Intent intent = new Intent(this, IntentService1.class);
    intent.putExtra("messenger", new Messenger(handler));
    startService(intent);

Inside IntentService:

Bundle bundle = intent.getExtras();
if (bundle != null) {
    Messenger messenger = (Messenger) bundle.get("messenger");
    Message msg = Message.obtain();
    msg.setData(data); //put the data here
    try {
        messenger.send(msg);
    } catch (RemoteException e) {
        Log.i("error", "error");
    }
}

or

If you want to use the BroadcastReceiver here is good example for using that.

Community
  • 1
  • 1
Kartheek
  • 7,104
  • 3
  • 30
  • 44
  • I think broadcast is easy to implement and understand but need to take care or register and unregister of broadcast..So better one should implement Handler . – Shakeeb Ayaz Jun 23 '15 at 06:58
  • Ya you are right but if the service is running continuously in the background then broadcast receiver is a better option – Kartheek Jun 23 '15 at 07:15
  • Hi kartheek I dont think that you can send messenger object in put extras ,it dose not support – Shakeeb Ayaz Jun 23 '15 at 07:32
  • You can send a Messenger through intent because it implements Parcelable. you can this link http://stackoverflow.com/questions/20796612/how-to-send-a-message-from-an-intent-service-to-an-activity – Kartheek Jun 23 '15 at 09:18