Reputation: 304
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
Reputation: 4344
Two ways,
like:
public Handler MyHandler= new Handler() {
public void handleMessage(android.os.Message msg) {
// Update your 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