Android use string from activity in another activity Asynctask (Void) -
i have app need send string 1 activity , use string in asynctask part of activity. succesfully sent string , receive in oncreate part of second activity, in asynctask, string need part of feed url, receiving null value. gcmintentservices.java activity sends string androidrssreader activity, string msg1.
gcmintentservices.java:
package com.andrija.bladimircodewp; import com.google.android.gms.gcm.googlecloudmessaging; import android.app.intentservice; import android.app.notification; import android.app.notificationmanager; import android.app.pendingintent; import android.app.taskstackbuilder; import android.content.context; import android.content.intent; import android.media.ringtonemanager; import android.os.bundle; import android.os.systemclock; import android.provider.settings; import android.support.v4.app.notificationcompat; import android.util.log; import android.widget.toast; public class gcmintentservice extends intentservice { public static final int notification_id = 1; private static final string tag = "gcmintentservice"; private notificationmanager mnotificationmanager; notificationcompat.builder builder; context context; public static final int notifyid = 9001; string msg1; string msg2; public gcmintentservice() { super("gcmintentservice"); } @override protected void onhandleintent(intent intent) { bundle extras = intent.getextras(); googlecloudmessaging gcm = googlecloudmessaging.getinstance(this); string msg1 = intent.getstringextra("cat_name"); string msg2 = intent.getstringextra("message"); // string msg = intent.getstringextra("message"); // string msg = msg1 + ":" + msg2; // getmessagetype() intent parameter must intent received // in broadcastreceiver. string messagetype = gcm.getmessagetype(intent); if (!extras.isempty()) { // has effect of unparcelling bundle /* * filter messages based on message type. since gcm * extended in future new message types, ignore * message types you're not interested in, or don't * recognize. */ if (googlecloudmessaging. message_type_send_error.equals(messagetype)) { sendnotification("send error: " + extras.tostring()); } else if (googlecloudmessaging. message_type_deleted.equals(messagetype)) { sendnotification("deleted messages on server: " + extras.tostring()); // if it's regular gcm message, work. } else if (googlecloudmessaging. message_type_message.equals(messagetype)) { // loop represents service doing work. (int i=0; i<5; i++) { log.i(tag, "working... " + (i+1) + "/5 @ " + systemclock.elapsedrealtime()); try { thread.sleep(5000); } catch (interruptedexception e) { } } log.i(tag, "completed work @ " + systemclock.elapsedrealtime()); // post notification of received message. // sendnotification(extras.getstring("notice")); sendnotification(msg1); log.i(tag, "received: " + extras.tostring()); } } // release wake lock provided wakefulbroadcastreceiver. gcmbroadcastreceiver.completewakefulintent(intent); } private void sendnotification(string msg1) { /* intent resultintent = new intent(this, androidrssreader.class); resultintent.putextra("message", msg); pendingintent resultpendingintent = pendingintent.getactivity(this, 0, resultintent, pendingintent.flag_update_current); */ intent resultintent = new intent(this, androidrssreader.class); bundle b = new bundle(); // b.putstring("message", msg); b.putstring("cat_name", msg1); // resultintent.putextra("cat_name", msg1); resultintent.putextras(b); resultintent.addflags(intent.flag_activity_new_task | intent.flag_activity_single_top); pendingintent resultpendingintent = pendingintent.getactivity(getapplicationcontext(), 0, resultintent, pendingintent.flag_update_current); notificationcompat.builder mnotifybuilder; notificationmanager mnotificationmanager; mnotificationmanager = (notificationmanager) getsystemservice(context.notification_service); mnotifybuilder = new notificationcompat.builder(this) // .setcontenttitle("alert") // .setcontenttext("you've received new message.") .setcontenttitle("") .setcontenttext(msg1) .setstyle(new notificationcompat.bigtextstyle().bigtext(msg1)) .setsmallicon(r.drawable.gcm_cloud); // set pending intent mnotifybuilder.setcontentintent(resultpendingintent); // set vibrate, sound , light int defaults = 0; defaults = defaults | notification.default_lights; defaults = defaults | notification.default_vibrate; defaults = defaults | notification.default_sound; mnotifybuilder.setdefaults(defaults); // set content notification // mnotifybuilder.setcontenttext("new message server"); // set autocancel mnotifybuilder.setautocancel(true); // post notification mnotificationmanager.notify(notifyid, mnotifybuilder.build()); } }
and activity receiving string another, androidrss reader.java:
package com.andrija.bladimircodewp; import java.io.ioexception; import java.net.malformedurlexception; import java.net.url; import javax.xml.parsers.parserconfigurationexception; import javax.xml.parsers.saxparser; import javax.xml.parsers.saxparserfactory; import org.xml.sax.inputsource; import org.xml.sax.saxexception; import org.xml.sax.xmlreader; import android.app.progressdialog; import android.app.listactivity; import android.content.context; import android.content.intent; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.arrayadapter; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class androidrssreader extends listactivity { private rssfeed myrssfeed = null; // textview mojtext = null; // public string msg1 = "katarina"; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mainrss); // mojtext = (textview)findviewbyid(r.id.moj_text); bundle extras = getintent().getextras(); // if(extras != null){ // string msg = extras.getstring("message"); string msg1 = extras.getstring("cat_name"); // mojtext.settext("evo titla : " + msg); // toast.maketext(androidrssreader.this, msg1, toast.length_long).show(); // new mytask().execute(); mytask task = new mytask(); task.msg1 = extras.getstring("cat_name"); task.execute(); // } } private class mytask extends asynctask<void, void, void>{ public string msg1; public mytask (string msg1) { this.msg1 = msg1; } @override protected void doinbackground(void... arg0) { try { // url rssurl = new url("http://160.99.61.22/pushapp/category/katarina/feed/"); url rssurl = new url("http://160.99.61.22/pushapp/category/" + msg1 + "/feed/"); saxparserfactory mysaxparserfactory = saxparserfactory.newinstance(); saxparser mysaxparser = mysaxparserfactory.newsaxparser(); xmlreader myxmlreader = mysaxparser.getxmlreader(); rsshandler myrsshandler = new rsshandler(); myxmlreader.setcontenthandler(myrsshandler); inputsource myinputsource = new inputsource(rssurl.openstream()); myxmlreader.parse(myinputsource); myrssfeed = myrsshandler.getfeed(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (parserconfigurationexception e) { e.printstacktrace(); } catch (saxexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(void result) { if (myrssfeed!=null) { textview feedtitle = (textview)findviewbyid(r.id.feedtitle); textview feedsadrzajposta = (textview)findviewbyid(r.id.feedsadrzaj_posta); textview feedpubdate = (textview)findviewbyid(r.id.feedpubdate); textview feedlink = (textview)findviewbyid(r.id.feedlink); feedtitle.settext(myrssfeed.gettitle()); feedsadrzajposta.settext(myrssfeed.getsadrzajposta()); feedpubdate.settext(myrssfeed.getpubdate()); feedlink.settext(myrssfeed.getlink()); arrayadapter<rssitem> adapter = new arrayadapter<rssitem>(getapplicationcontext(), r.layout.list_black_text,r.id.list_content,myrssfeed.getlist()); setlistadapter(adapter); toast.maketext(getapplicationcontext(), "ggg: " + msg1, toast.length_short).show(); }else{ textview textempty = (textview)findviewbyid(android.r.id.empty); textempty.settext("no feed found!"); } super.onpostexecute(result); } } @override protected void onlistitemclick(listview l, view v, int position, long id) { // todo auto-generated method stub intent intent = new intent(this,showdetails.class); bundle bundle = new bundle(); bundle.putstring("keytitle", myrssfeed.getitem(position).gettitle()); bundle.putstring("keysadrzajposta", myrssfeed.getitem(position).getsadrzaj_posta()); bundle.putstring("keylink", myrssfeed.getitem(position).getlink()); bundle.putstring("keypubdate", myrssfeed.getitem(position).getpubdate()); intent.putextras(bundle); startactivity(intent); } }
why in doinbackground part of asynctask string msg1 null?
pass param async task instead, below:
mytask task = new mytask(); t.doinbackground("hello"); private class mytask extends asynctask<string, void, void> { @override protected void doinbackground(string... params) { string msg1 = params[0]; return null; } }
Comments
Post a Comment