java - Call ASyncTask for notification -
i want use asynctask on app sending notification user when on website, json value =\= false.
the asynctask as:
public class asynctask extends asynctask<string, void, string> { private static boolean onair; public static final string tag = "[notif]"; private final static int interval = 1000; //2 minutes handler mhandler = new handler(); private final httpclient httpclient = new defaulthttpclient(); final httpparams params = httpclient.getparams(); httpresponse response; private string content = null; private boolean error = false; private context mcontext; private int notification_id = 1; private notification mnotification; private notificationmanager mnotificationmanager; runnable mhandlertask = new runnable() { @override public void run() { detectonline_prepare(); mhandler.postdelayed(mhandlertask, interval); } }; public asynctask(context context){ this.mcontext = context; //get notification manager mnotificationmanager = (notificationmanager) mcontext.getsystemservice(context.notification_service); } protected void onpreexecute() { log.i(tag, "erreur onpretexecute"); } protected string doinbackground(string... urls) { detectonline_prepare(); return content; } protected void oncancelled() { createnotification("une erreur s'est produite",content); } protected void onpostexecute(string content) { detectonline_prepare(); if (error) { log.i(tag, "erreur onpostexecute"); } else { log.i(tag, "[notif] ok"); createnotification("ok","ok"); } } private void createnotification(string contenttitle, string contenttext) { //build notification using notification.builder notification.builder builder = new notification.builder(mcontext) .setsmallicon(android.r.drawable.stat_sys_download) .setautocancel(true) .setcontenttitle(contenttitle) .setcontenttext(contenttext); //get current notification mnotification = builder.getnotification(); //show notification mnotificationmanager.notify(notification_id, mnotification); } public void detectonline_prepare(){ strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); try { detectonline(); } catch (jsonexception | ioexception e) { e.printstacktrace(system.out); } } public void detectonline() throws jsonexception, ioexception { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); checkifonline(); if (onair == false) { log.i(tag, "variable off"); } else { //createnotification(); log.i(tag, "variable on"); } } public static void checkifonline() throws jsonexception, ioexception { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); string channerurl = "https://api.dailymotion.com/video/x3p6d9r?fields=onair"; string jsontext = readfromurl(channerurl);// reads text url jsonobject json = new jsonobject(jsontext); // create json object string jsontext if (json.has("onair")) { // simple test check node need existe //boolean onair = json.getboolean("onair"); // in url gave onair value boolean type onair = json.getboolean("onair"); return; } } private static string readfromurl(string url) throws ioexception { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); url page = new url(url); stringbuilder sb = new stringbuilder(); scanner scanner = null; try { scanner = new scanner(page.openstream()); while (scanner.hasnextline()) { sb.append(scanner.nextline()); } } { if (scanner != null) scanner.close(); } return sb.tostring(); }
but want know how work. how can call mainactivity asynctask (for work in background).
thank you
asyntask threads let operations in separate thread (non-ui main thread). don't need asyntask notifications, can achieved using pendingintent , listeners phone call, touch listener, etc.
now coming question try this:
in main thread following:
asynstask myasynctask = new asynctask(getapplicationcontext()); myasynctask.execute();
tutorial: good read
Comments
Post a Comment