Not Listen PhoneListener after Abnormal Terminatio

2019-06-14 14:18发布

问题:

I am working on application that tracks the InComing/Outgoing calls & save calls records into Database. Yesterday I seen a scenario In which application is terminated by OS & a warning message pops Up (Error: Application is not responding). After application is terminated by OS. Application is not able to listen In Coming/Outgoing calls again. I think, Application Phone Listener is unregistered by abnormal application termination. Os calls System.Exit(0) for application termination

If I follow this approach :-

public static void main(String[] args) 
{
    /** 
     * Registering the phone Listener. 
     * */
    Phone.addPhoneListener( new ConcretePhoneListener() );

   new SampleUIApp().enterEventDispatcher();
   }

In this case every time at the application start up , phone listener is registered. And Application's (Phone Listener) registered multiple times. Means for a single In/Out Call I am getting multipule times

void callConnected( int aCallId ) 
callDisconnected( int aCallId ) 

for sourt out this issue , I am using RunTime Store Management System. For this I am using Following approach :-

private static void registeringPhoneListener() 
{
    RuntimeStore mRuntimeStore = RuntimeStore.getRuntimeStore();
    final long GUID = 0xba9e3b33ac5fe37eL;

    String msPhoneListenerString = null;

    if(mRuntimeStore.get(GUID) != null)
    {
        Log.debug(  "PhoneListener is Already Implemented ## ");
    }
    else
    {
        Phone.addPhoneListener( new ConcretePhoneListener() );
        mRuntimeStore.put(GUID, "PhoneListener");   

          Log.debug("PhoneListener Implemented First Time ## " );           
    }
}

this Approach is working fine until I get abnormal termination by OS, Because RuntimeStoreManagemnet is not null but Application Phone-listener is Derigester.

please Help me out this.

回答1:

There is more going on here than you have shown us, so I am not convinced that this is the correct answer. What I am confident on is that you need to change your code to make this work properly for you.

I suspect you are misusing the listener processing.

Normally listeners run in the context of the application that they are listening on. So in fact your listener is running under control of the Phone application. You do not want to break this (as it appears you are doing) in your listener code. So you should make your listener code as tight (efficient and unbreakable) as you possibly can. In this case, from memory, the listener runs with the Event Thread, so in addition to making sure your code is tight, you shouldn't do any blocking operations - and remember reading a database could count as a blocking operation.

The general recommendation is to make sure listeners do as little as possible - the best recommendation is that they kick off a global event and the heavy processing is handled in your application.

Here is an article about Global Events:

http://supportforums.blackberry.com/t5/Java-Development/Global-Events-and-Global-Event-Listeners/ta-p/444814

Having discussed the theory, what is happening here?

In this case I suspect your listener is blocking and you are taking down the Phone application. I am pretty sure the Phone application gets restarted by the system (what use is the phone without the phone application!) so you might not notice this. But when the Phone Application is restarted, it does not have your listener - and so you stop getting notifications.

So I think you need to fix your Listener code to make sure it stops taking down the Phone application.

I suspect you have two choices:

  1. move the processing to your own application using a Global Event (my recommendation)
  2. move the blocking operations off the Event Thread.

Implementing either should stop your application breaking the Phone Application and the problem will be resolved.