android - How can I trigger my notification from a method? -
i trying trigger notification bottom. followed example code http://www.compiletimeerror.com/2013/10/status-bar-notification-example-in.html#.vwtos1xviko
i can trigger notification when call method homeactivity main activity app. when try call 1 of methods within app, nothing happens. (eg)
homeactivity homeactivity = new homeactivity(); homeactivity.notify("title: ...", "msg: ... ");
here logs event. trigger alert after 5 button presses
05-31 18:33:41.533 10118-10118/com.myapp.md e/>>>>>>﹕ multiclickevent clickcount = 5 05-31 18:33:41.543 10118-10118/com.myapp.md e/>>>>>>>﹕ in onactivation of hwreceiver 05-31 18:33:41.603 10118-10118/com.myapp.md v/vibrator﹕ called vibrate(long) api - puid: 10588, packagename: com.myapp.md 05-31 18:33:41.603 10118-10118/com.myapp.md v/vibrator﹕ vibrate - puid: 10588, packagename: com.myapp.md, ms: 3000, mag: -1 05-31 18:33:41.643 10118-10118/com.myapp.md d/abslistview﹕ onvisibilitychanged() called, visibility : 4 05-31 18:33:41.643 10118-10118/com.myapp.md d/abslistview﹕ unregisterirlistener() called 05-31 18:33:41.643 10118-10118/com.myapp.md d/abslistview﹕ onvisibilitychanged() called, visibility : 4 05-31 18:33:41.643 10118-10118/com.myapp.md d/abslistview﹕ unregisterirlistener() called 05-31 18:33:41.643 10118-11068/com.myapp.md e/>>>>>>﹕ in currentlocationprovider constructor - trying retrieve location getlastknownlocation() 05-31 18:33:41.833 10118-11068/com.myapp.md i/com.myapp.md.alert.smsadapter﹕ sms sent: need immediate help! - i'm here https://maps.google.com/maps?q=40.7348922,-73.977548 via network 05-31 18:33:44.883 10118-10118/com.myapp.md e/>>>>>>>﹕ in onreceive of hwreceiver 05-31 18:33:44.883 10118-10118/com.myapp.md e/>>>>>>>﹕ in onreceive of hwreceiver context com.myapp.md.trigger.hardwaretriggerservice@429b89b0 05-31 18:33:44.883 10118-10118/com.myapp.md e/>>>>>>>﹕ in onreceive of hwreceiver intent intent { act=android.intent.action.screen_on flg=0x50000010 }
i tried put notification in smsadapter
public class smsadapter { private static final string log_tag = smsadapter.class.getname(); public void sendsms(context context, string phonenumber, string message) { // log.e("20140411", "sending fake sms -> " + message); if(!applicationsettings.isfirstmsgsent(context)){ applicationsettings.setfirstmsgsent(context, true); } smsmanager smsmanager = getsmsmanager(); try { smsmanager.sendtextmessage(phonenumber, null, message, null, null); log.i(log_tag, "sms sent: " + message); homeactivity homeactivity = new homeactivity(); homeactivity.notify("title: text sos", "msg: alerting emergency contacts "); } catch (exception exception) { log.e(log_tag, "sending sms failed " + exception.getmessage()); } } smsmanager getsmsmanager() { return smsmanager.getdefault(); } }
when message
05-31 18:39:33.163 18640-19982/com.textsosalert.md e/>>>>>>﹕ in currentlocationprovider constructor - trying retrieve location getlastknownlocation() 05-31 18:39:33.383 18640-19982/com.textsosalert.md i/com.textsosalert.md.alert.smsadapter﹕ sms sent: need immediate help! - i'm here https://maps.google.com/maps?q=40.7348689,-73.9775756 via network 05-31 18:39:33.383 18640-19982/com.textsosalert.md e/com.textsosalert.md.alert.smsadapter﹕ sending sms failed can't create handler inside thread has not called looper.prepare()
here notify method defined:
@suppresswarnings("deprecation") public void notify(string notificationtitle, string notificationmessage) { notificationmanager notificationmanager = (notificationmanager) getsystemservice(notification_service); @suppresswarnings("deprecation") notification notification = new notification(r.drawable.icon_sos, "new message", system.currenttimemillis()); intent notificationintent = new intent(this, homeactivity.class); pendingintent pendingintent = pendingintent.getactivity(this, 0, notificationintent, 0); notification.setlatesteventinfo(homeactivity.this, notificationtitle, notificationmessage, pendingintent); notificationmanager.notify(9999, notification); }
the problem you're calling new homeactivity()
, should never do. activities in android started using startactivity()
or startactivityforresult()
, , should never created explicitly new
.
when call new homeactivity()
, none of activity lifecycle callbacks called, activity not have valid context.
so, won't work:
homeactivity homeactivity = new homeactivity(); homeactivity.notify("title: ...", "msg: ... ");
if want have method can re-use in different places in code, put in separate class, , have method take context parameter.
import android.app.notification; import android.app.notificationmanager; import android.app.pendingintent; import android.content.context; import android.content.intent; public class mynotification { @suppresswarnings("deprecation") public static void notify(context context, string notificationtitle, string notificationmessage) { notificationmanager notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); @suppresswarnings("deprecation") notification notification = new notification(r.drawable.icon_sos, "new message", system.currenttimemillis()); intent notificationintent = new intent(context, homeactivity.class); pendingintent pendingintent = pendingintent.getactivity(context, 0, notificationintent, 0); notification.setlatesteventinfo(context, notificationtitle, notificationmessage, pendingintent); notificationmanager.notify(9999, notification); } }
then in smsadapter, have:
try { smsmanager.sendtextmessage(phonenumber, null, message, null, null); log.i(log_tag, "sms sent: " + message); /* don't this: homeactivity homeactivity = new homeactivity(); homeactivity.notify("title: text sos", "msg: alerting emergency contacts "); */ //do instead: mynotification.notify(context, "title: text sos", "msg: alerting emergency contacts "); } catch (exception exception) { log.e(log_tag, "sending sms failed " + exception.getmessage()); }
also note can call homeactivity using this
context
:
mynotification.notify(this, "title: text sos", "msg: alerting emergency contacts ");
Comments
Post a Comment