donison24x7
donison24x7

Reputation: 304

Update Activity/Trigger an Event when a Broadcast is Received while the activity is open

I have made a broadcast receiver which notifies when a message is received from server. When I click on the notification it opens the Activity intended and also update the message list.

My question is how to update the message list (fire an event) when the broadcast is received while the activity is open? (just like they do in 'WhatsApp', they don't send notification while chatting but rather update the message list).

My broadcast receiver is as follows:

public class NotifReceiver extends BroadcastReceiver {
    private Context context;
    private String notifTitle,notifStatus;
    public static int NOTIF_ID = 54321;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;

         if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            context.startService(new Intent(context, NotifService.class));
            Log.i("Aditya", "Background Service of SamajApp Started");
         }
        if(intent.getAction().equals("Aditya.Notification")){
            showNotif();
        }
    }

    public void showNotif(){
        NotificationManager notifManager = NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent i = new Intent(context, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pIntent = PendingIntent.getActivity(context, 0, i, 0);

        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notif = new NotificationCompat.Builder(context);
        notif.setContentTitle("SamajApp");
        notif.setContentText(notifTitle);
        notif.setSmallIcon(R.drawable.logo);
        notif.setSound(soundUri);
        notif.setContentIntent(pIntent);
        notifManager.notify(NOTIF_ID, notif.build());
    }
}

I have registered it in manifest and it is working perfectly.

My MainActivity is as follows:

 public class MainActivity extends Activity {
       private ListView mListView;

       @Override
        protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main); 
             mListView = (ListView) findViewById(R.id.listViewHome);

             updateList();

        }

        //MsgListAdapter is my custom Adapter to populate Listview,
        private void updateList(){
        registerForContextMenu(mListView);
        Vector<HashMap<String, String>> list = getMsgListfromDB();  //gets message list from database
        Vector<HashMap<String, byte[]>> blobList = getBlobListfromDB();  //gets blob list from db.
        ad = new MsgListAdapter(getApplicationContext(), list,blobList,R.layout.listrow,
                new String[] { "MsgCat", "MsgDt_Simp","by","FlagUnread"},
                new int[] {R.id.textViewCat, R.id.textViewDateTime,R.id.textViewBy},
                new int[] {R.id.imageViewMsgImg,R.id.imageViewCatImg},R.id.imageViewUnreaddr);
        mListView.setAdapter(ad);
            }
       }

Any help regarding this will be appreciated. thank you.

Upvotes: 0

Views: 2026

Answers (1)

Techfist
Techfist

Reputation: 4344

Two ways,

  1. Either have a static Handler declartion in your Activity, and using the same post a message back to activity, if alive update whatever you need.

like:

public Handler MyHandler= new Handler() {
    public void handleMessage(android.os.Message msg) {
        // Update your UI
    }

};
  1. Declare your broadcast receiver as a static subclass inside your Activity class, so once you receive an subsequent broadcast, you can easily update you UI.

Like:

public class MainActivity extends Activity {


    private MyReceiver mReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }



    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        IntentFilter mFilter = new IntentFilter();
        // add action to this filters

        // Initialize ur receiver
        mReceiver = new MyReceiver();
        // register when activity is resumed
        registerReceiver(mReceiver, mFilter);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        // unregister when activity is paused
        if(mReceiver != null){
            unregisterReceiver(mReceiver);
        }
    }



    class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            // This is ur receiver do whatever you want here wIth UI

        }

    }

In my opinion second one would be easy and better to use.

Upvotes: 1

Related Questions