0

I wrote a messaging application and I want to notify users when some other user message them. But I cannot keep track of it in my ContactListActivity so I try to show it in my connection.java class.(Simply, pop-up a text when viewing contactList if a new message arrives). Here is what I tried to do

private  final Context mApplicationContext;
public Connection(context c){
 mApplicationContext = c.getApplicationContext();
/....
}
  System.out.println("Print something");
  Toast.makeText(mApplicationContext, " You received a  message from "+contactJid, Toast.LENGTH_LONG).show(); //Is called everytime when I receive a message.
  System.out.printnl("Print something");  // I can print both, I receive the message but toast does not appear

Dont worry about the variable names. How can I accomplish my goal? I use mApplicationContext in other places and it does what I want it to do.

I also tried with creating a new ContactListActivity object in this class and get its application context it also failed Constructive feedback is appreciated so I can make the question clearer.

Prethia
  • 1,183
  • 3
  • 11
  • 26
  • You getting response here? and why you are sending context from outside then using getApplicationContext() on it. Can you show from where you are calling Connection() – Rahul Dec 25 '16 at 19:58
  • Hmm seems like I wrote the wrong thing I am updating, it should be c.getApplicationContext(). Toast is called everytime when I receive a new message – Prethia Dec 25 '16 at 20:08
  • Did you print log and verified you are getting response – Rahul Dec 25 '16 at 20:10
  • yes I did everything else works fine and I get the message. I wrote different logD's and saw each of them but toast did not appear. – Prethia Dec 25 '16 at 20:11
  • Just a confirmation show notification option is enabled for your app – Rahul Dec 25 '16 at 20:14
  • Well in my chat activity if an another user sends me a message I can use toast to show it so I am sure it also works. Thanks for trying to find where I might had a blunder but I do not think I did one. Do you think code is ok? – Prethia Dec 25 '16 at 20:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131450/discussion-between-rahul-kapoor-and-prethia). – Rahul Dec 25 '16 at 20:19

1 Answers1

1

If you are getting response in Logs then it might be an issue of non UI thread. Nothing will update or show on UI from worker thread so update your UI from main thread only.

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
    @Override
    public void run() {
    //Here you can update your UI
    }
});
Rahul
  • 10,457
  • 4
  • 35
  • 55