android - Error while sending data from service to activity -
my application described follows: application receive push information server , displayed on notification bar of android. when click on notification of application load url server. problem now, can not send url received server original activity.
below error when run application.
04-09 11:25:26.503: e/androidruntime(1030): fatal exception: intentservice[gcmintentservice-810012644757-1] 04-09 11:25:26.503: e/androidruntime(1030): android.util.androidruntimeexception: calling startactivity() outside of activity context requires flag_activity_new_task flag. want? 04-09 11:25:26.503: e/androidruntime(1030): @ android.app.contextimpl.startactivity(contextimpl.java:624) 04-09 11:25:26.503: e/androidruntime(1030): @ android.content.contextwrapper.startactivity(contextwrapper.java:258) 04-09 11:25:26.503: e/androidruntime(1030): @ com.ketan.demo.gcmintentservice.generatenotification(gcmintentservice.java:118) 04-09 11:25:26.503: e/androidruntime(1030): @ com.ketan.demo.gcmintentservice.onmessage(gcmintentservice.java:70) 04-09 11:25:26.503: e/androidruntime(1030): @ com.google.android.gcm.gcmbaseintentservice.onhandleintent(gcmbaseintentservice.java:179) 04-09 11:25:26.503: e/androidruntime(1030): @ android.app.intentservice$servicehandler.handlemessage(intentservice.java:59) 04-09 11:25:26.503: e/androidruntime(1030): @ android.os.handler.dispatchmessage(handler.java:99) 04-09 11:25:26.503: e/androidruntime(1030): @ android.os.looper.loop(looper.java:130) 04-09 11:25:26.503: e/androidruntime(1030): @ android.os.handlerthread.run(handlerthread.java:60)
my code:
private static void generatenotification(context context, string message) { int icon = r.drawable.ic_stat_gcm; long when = system.currenttimemillis(); notificationmanager notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); notification notification = new notification(icon, message, when); string title = context.getstring(r.string.app_name); intent notificationintent = new intent().setclass(context.getapplicationcontext(), demoactivity.class); // set intent not start new activity notificationintent.setflags(intent.flag_activity_multiple_task); notificationintent.setflags(intent.flag_activity_new_task); notificationintent.setflags(intent.flag_activity_clear_top | intent.flag_activity_single_top); pendingintent intent = pendingintent.getactivity(context, 0, notificationintent, 0); notification.setlatesteventinfo(context, title, message, intent); notification.flags |= notification.flag_auto_cancel; notificationmanager.notify(0, notification); notificationintent.putextra("url", message); // launch new activity , add additional flags intent context.getapplicationcontext().startactivity(notificationintent); /*intent = new intent().setclass(context.getapplicationcontext(), demoactivity.class); i.setflags(intent.flag_activity_multiple_task); i.setflags(intent.flag_activity_new_task); i.putextra("url", message); // launch new activity , add additional flags intent context.getapplicationcontext().startactivity(i);*/ }
activity demo
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); checknotnull(server_url, "http://10.0.2.2:8080/gcm-demo-server/"); // checknotnull(sender_id, "972801588344"); // make sure device has proper dependencies. gcmregistrar.checkdevice(this); // make sure manifest set - comment out line // while developing app, uncomment when it's ready. gcmregistrar.checkmanifest(this); setcontentview(r.layout.main); bundle extras = getintent().getextras(); if (extras != null) { url = extras.getstring("url"); } web=(webview) findviewbyid(r.id.webview1); if(url==null) { web.loadurl("http://kqxs.vn/trang_chu"); }else { web.loadurl(url); } }
i fixed following by
private static void generatenotification(context context, string message) { int icon = r.drawable.ic_stat_gcm; long when = system.currenttimemillis(); notificationmanager notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); notification notification = new notification(icon, message, when); string title = context.getstring(r.string.app_name); intent notificationintent = new intent(context, demoactivity.class); // set intent not start new activity notificationintent.setflags(intent.flag_activity_clear_top | intent.flag_activity_single_top); notificationintent.putextra("url", message); pendingintent intent = pendingintent.getactivity(context, 0, notificationintent, 0); notification.setlatesteventinfo(context, title, message, intent); notification.flags |= notification.flag_auto_cancel; notificationmanager.notify(0, notification); }
but have problem, application load correct url first time, 2nd, after receiving new url server, application loaded url of first time. have tried , tried many times still getting results so...
please see following question:
android:what difference between setflags , addflags intent
intent.setflags :
means replacing old flags adding new flags intent
intent.addflags :
means appending new flags added intent
so, need use notificationintent.addflags
instead of notificationintent.setflags
appending intent.flag_activity_new_task flag notificationintent
intent as:
....... notificationintent.setflags(intent.flag_activity_multiple_task); notificationintent.addflags(intent.flag_activity_new_task); notificationintent.addflags(intent.flag_activity_clear_top | intent.flag_activity_single_top); .......
Comments
Post a Comment