hkguile
hkguile

Reputation: 4359

update textview title in an Activity from a service

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?

Upvotes: 2

Views: 4618

Answers (2)

Shakeeb Ayaz
Shakeeb Ayaz

Reputation: 6096

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)

Upvotes: 3

Kartheek
Kartheek

Reputation: 7214

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.

Upvotes: 0

Related Questions