java - Using Thread in Android for sending message -
java - Using Thread in Android for sending message -
i have code:
if (value) { thread = new thread() { @override public void run() { seek { while (!isconnected()) { synchronized (this) { wait(3000); } } } grab (interruptedexception ex) { } if(wifimanager.iswifienabled()){ sendmessagewidget(); } else { showwifisettingsalert(); } } }; thread.start(); }
i want app wait until google api client connected , send message.
the code isconnected method is:
public boolean isconnected() { mgoogleapiclient.connect(); if (mgoogleapiclient.isconnected()) { homecoming true; } homecoming false; }
but error message: nullpointerexception: can't create handler within thread has not called looper.prepare(), , says error somewhere id showwifisettingsalert()
here code:
public void showwifisettingsalert() { alertdialog.builder alertdialog = new alertdialog.builder(getactivity()); // setting dialog title alertdialog.settitle("location accuracy tips"); // setting dialog message alertdialog .setmessage("you can improve accuracy of location turning on\n- wi-fi"); // on pressing settings button alertdialog.setpositivebutton("turn on", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { wifimanager.setwifienabled(true); // posalji poruke al pre toga jos jednom azuriraj // lokaciju al ako je pozvana aplikacija iz widgeta if (value) { sendmessagewidget(); } } }); // on pressing cancel button alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { wifimanager.setwifienabled(false); // posalji poruke al pre toga jos jednom azuriraj // lokaciju al ako je pozvana aplikacija iz widgeta if (value) { sendmessagewidget(); } } }); // showing alert message alertdialog.show(); }
i want, if wifi not enabled, user take enable or not, either way message should sent... can help please?
since can't touch ui thread other main thread, must post these changes ui thread , looper , associated handlers. can explicitly creating handler associated ui thread (which work anywhere, since looper.getmainlooper()
static call) such as:
if (value) { handler uicallback = new handler(looper.getmainlooper()); thread = new thread() { @override public void run() { seek { while (!isconnected()) { synchronized (this) { wait(3000); } } } grab (interruptedexception ex) { } uicallback.post(new runnable() { @override public void run() { if(wifimanager.iswifienabled()){ sendmessagewidget(); } else { showwifisettingsalert(); } } }); } }; thread.start(); }
or instead of using handler @ all, can wrap part in run()
method in runonuithread()
if in activity same thing.
you should note however, don't need utilize threading here. if follow illustration on: https://developer.android.com/google/auth/api-client.html you'll find implementing connectioncallbacks, onconnectionfailedlistener
can phone call mgoogleapis.connect()
activity's onstart()
, when connects or fails corresponding callback executed on calling thread. instance,
@override public void onconnected(bundle connectionhint) { if(wifimanager.iswifienabled()){ sendmessagewidget(); } else { showwifisettingsalert(); } }
achieves same thing...
java android multithreading alertdialog
Comments
Post a Comment