BlackBerry Looking up email address
I am working on a BlackBerry Application that is going to lookup the email address registered to the device. I am us开发者_如何学运维ing this code to accomplish:
Session session = Session.getDefaultInstance();
System.out.println("############### got session ################");
if (session != null) {
Store store = session.getStore();
System.out.println("################ got store ######################");
ServiceConfiguration serviceConfig = store.getServiceConfiguration();
System.out.println("################ got config #####################");
email = serviceConfig.getEmailAddress();
}
This works perfectly for devices that have already registered an email address. But if the device doesn't have an address registered to it this line:
email = serviceConfig.getEmailAddress();
Never returns. So my entire application stalls out indefinitely. What is the best approach for solving this? My first idea is set a timer task that will set the email String to some default value such as "No Address Registered" after 1 or 2 seconds. Is there some better way to get notified that there was no email present other than the app just stalling out and doing nothing?
It turns out that serviceConfig.getEmailAddress();
throws an exception if there is no email registered. There is no mention of this in the BlackBerry documentation. It is also strange because if you do not catch that exception the method just never returns and your application will stall indefinitely. But to fix you can just surround that call with try/catch and set the email to default in the catch block.
Would have saved me a lot of time if this was actually documented correctly. I am hopefully that posting the answer here will save someone from chasing something that is undocumented.
精彩评论