Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: ControlledAccessException trying to use LocationProvider.getInstance(...)

OK, sounds like time to knock up a small background standalone application to demo the problem.  Can you do that?  Happy to help, but distracted by other things at the moment. 

Contributor
Posts: 33
Registered: ‎02-13-2010
My Device: 9800
My Carrier: Rogers

Re: ControlledAccessException trying to use LocationProvider.getInstance(...)

Yeah, I've done that already and trying to communicate with official RIM support. Haven't heard back from them yet...

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: ControlledAccessException trying to use LocationProvider.getInstance(...)

Can you post it here?

Contributor
Posts: 33
Registered: ‎02-13-2010
My Device: 9800
My Carrier: Rogers

Re: ControlledAccessException trying to use LocationProvider.getInstance(...)

[ Edited ]

Well, I made some more progress. The issue only happens if I trigger the GPS call from a folder listener event. The background application can call the GPS just fine using other methods (for example a Timer). 

 

I'll try to clean-up the application I have now and attach it.

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: ControlledAccessException trying to use LocationProvider.getInstance(...)

"The issue only happens if I trigger the GPS call from a folder listener event"

 

That makes sense.  At that time you are running under control of another application, presumably the mail application.  You are probably running in the background too. 

 

I would strongly recommend you stop doing this.

 

In fact in general I would recommend that you do not issue blocking calls or invoke 'secure' APIs when running a listener.  You are blocking or interrupting some other application, possibly at a point that it does not expect or want this to happen. 

 

In general I recommend that listeners are coded to just collect "listened too events", and the data sent, typically using a GlobalEvent, to your own application for processing. 

 

If you do this, then things like this, security prompt and any User interaction are done by your application, not a piece of 'system' software that is running your code.  This is much safer and ensures that you don't do things that break the basic processing of the BlackBerry.  Users are not going to like it if their mail stops working after they install your application for example. 

 

Does this make sense?

Contributor
Posts: 33
Registered: ‎02-13-2010
My Device: 9800
My Carrier: Rogers

Re: ControlledAccessException trying to use LocationProvider.getInstance(...)

Hi Peter,

 

Thanks for the reply. Of course, I'm just being a bit simplistic about what my application is doing and I won't be issuing any blocking calls when I'm listening to folder events.

 

At the moment I'm trigerring a new thread and then a timer to run my GPS call. I'll look into GlobalEvents which I haven't used yet.

 

Thanks.

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: ControlledAccessException trying to use LocationProvider.getInstance(...)

"I'm triggering a new thread and then a timer to run my GPS call."

As noted, with your current code, this will run under control of the folder listener application. 

 

By using a GlobalEvent, you can 'context shift' into your own application. 

 

There are other ways of doing it.  One way you might consider doing it, involves your Folder Listener having a reference to your application, then doing the following  will context switch into your application. 

 

<myapplication>.invokelater(new Runnable() {

public void run() {

// This code will run in your application context

}

});

 

You need to be extremely careful here though, because if your Application issues a System.exit(), then the Application will be stopped, but your listener will still retain a reference to it, which means it all stays in memory.  When the user starts your application again, well, you now have two copies of it in memory.  And then that copy will add its own Listener, and so it goes on.  This is a good way to get a Storage Leak.  You can get round this problem using a WeakReference. 

 

You might find this approach slightly easier than a Global Event and it actually achieves much the same thing. 

Contributor
Posts: 33
Registered: ‎02-13-2010
My Device: 9800
My Carrier: Rogers

Re: ControlledAccessException trying to use LocationProvider.getInstance(...)

Hi Peter,

 

I was already using the invokeLater. That's what I meant by triggering a new thread. For some reason though I still got the same error.

 

Anyway, I switched to a global event and all is well. Thanks so much for the help and tips. I really appreciate it!