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
nakumar97
Posts: 40
Registered: ‎08-13-2009
My Device: Not Specified
Accepted Solution

Unable to pushscreen from phonelistener

Hello all!

please help me on the following problem. i created an application which is working in background at the startup. i want to push a screen when any phone call is disconnected. i have added a phonelistener and wrote callDisconnected(callId) method. this method is called when the call is disconnected which is fine, but the problem is that no screen is displayed after the call disconnect. below is the code i am using for it.


============ Phone Listener ===============

public class CustomPhoneListener extends AbstractPhoneListener{

private Main theApp;
private PhoneCall call = null;


    public CustomPhoneListener(Main theApp){
        this.theApp=theApp;
    }


    public void callConnected(int callId) {       
       call = Phone.getCall(callId);
    }
   
       
    public void callDisconnected(int callId){

        if(call != null){
        System.out.println("???????-------Call Disconnected");
 
        theApp.requestForeground();          
       
        theApp.invokeLater(new Runnable(){
        public void run(){
            System.out.println("???????-------Inside RUN");
            theApp.pushScreen(new TagScreen());
        }});       
      
    }

}

 

============ Main Class ===============

public class Main extends UiApplication {   

private static Main theApp = null;
private Screen fScreen = null;
private static CustomPhoneListener customPhoneListener ;

    public static void main(String[] args) {       
        if ( args.length == 1 && args[0].equals( "autostartup" ) )
        {           
            // Create and register the object that will listen for Phone events.  Check for
            // ControlledAccessException as per page 69 of the BlackBerry Application
            // Developer Guide, Volume 2 (Version 4.0).
            try
            {
                theApp = new Main();
            }
            catch ( ControlledAccessException e )
            {
                Dialog.alert( "Access to Phone API restricted by system administrator." );
                System.exit( 1 );
            }
        }
        else /* THIS PART IS WORKING FINE */
        {
            theApp = new Main(new MenuScreen());
            theApp.enterEventDispatcher();
        }
       
        customPhoneListener = new CustomPhoneListener(theApp);
        Phone.addPhoneListener(customPhoneListener);       
 
    }

    public Main(Screen screen){
        pushScreen(screen);
    }

    public Main(){
    }

}

 

================= Tag Screen ==================

public class TagScreen extends MainScreen {
   
private LabelField labelField;

    public TagScreen(){
        setTitle("Tag Screen");
        labelField = new LabelField("Hello champ!");
        add(labelField);       
    }
}

------------------------------------------------------------------------------
The truth is not for all men, but only for those who seek it :smileyhappy:
------------------------------------------------------------------------------
Thanx and Regards
Naveen Kumar
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Unable to pushscreen from phonelistener

you create two application objects, but only one enters the event dispatcher.
you have the following options:
- start the app in your autostart part (i do this with my app, for example). a user click on the icon will bring it to the foreground
- use a global screen
- send a global event to your app (if it is running)

sharing one application object is theoretically possible using runtimestore, but i encountered only problems with it and would not advise to use this way.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
nakumar97
Posts: 40
Registered: ‎08-13-2009
My Device: Not Specified

Re: Unable to pushscreen from phonelistener

- start the app in your autostart part (i do this with my app, for example). a user click on the icon will bring it to the foreground

 how will i trap the click on icon. moreover i have different screens let say, SCREEN_1 which is opened when clicked on icon and SCREEN_2 which i want to open when a call is disconnected.

i will really appreciate if i can get some piece of code. so that it is easy to understand the things.

------------------------------------------------------------------------------
The truth is not for all men, but only for those who seek it :smileyhappy:
------------------------------------------------------------------------------
Thanx and Regards
Naveen Kumar
Developer
nakumar97
Posts: 40
Registered: ‎08-13-2009
My Device: Not Specified

Re: Unable to pushscreen from phonelistener

one more thing i noticed that invokeLater() is not called ever, SOP, inside the invokeLater() doesnt execute. 
------------------------------------------------------------------------------
The truth is not for all men, but only for those who seek it :smileyhappy:
------------------------------------------------------------------------------
Thanx and Regards
Naveen Kumar
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Unable to pushscreen from phonelistener

invokelater is not called because the application is not on the event dispatcher.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Unable to pushscreen from phonelistener

To be more precise, immediately after you register the PhoneListener on startup, your application terminates (main() method returns). So, your listener has a reference to an application that is no longer running. Posting an invokeLater to the application won't achieve what you want because the application is no longer running (in particular, as simon_hain said, the event dispatcher is no longer processing events posted to the application).
Developer
nakumar97
Posts: 40
Registered: ‎08-13-2009
My Device: Not Specified

Re: Unable to pushscreen from phonelistener

Thank you very much Simon and Klyubin.

                     it worked when i added EventDispatcher properly. so now my main class looks sumthing like the following:

 

public class Main extends UiApplication {   

private static Main theApp = null;

private static CustomPhoneListener customPhoneListener ;
    public static void main(String[] args) {       
        if ( args.length == 1 && args[0].equals( "autostartup" ) )
        {           
            // Create and register the object that will listen for Phone events.  Check for
            // ControlledAccessException as per page 69 of the BlackBerry Application
            // Developer Guide, Volume 2 (Version 4.0).
            try
            {
                theApp = new Main();
            }
            catch ( ControlledAccessException e )
            {
                Dialog.alert( "Access to Phone API restricted by system administrator." );
                System.exit( 1 );
            }
        }
        else
        {
            theApp = new Main(new MenuScreen());       
        }
       
        customPhoneListener = new CustomPhoneListener(theApp);
        Phone.addPhoneListener(customPhoneListener);       
        theApp.enterEventDispatcher();  
    }

    public Main(Screen screen){       
        pushScreen(screen);
    }

    public Main(){
     }
}

 

hope it helps other to solve the same problem.......

------------------------------------------------------------------------------
The truth is not for all men, but only for those who seek it :smileyhappy:
------------------------------------------------------------------------------
Thanx and Regards
Naveen Kumar
New Contributor
bhavi_bm
Posts: 3
Registered: ‎04-29-2011
My Device: 9550,9800

Re: Unable to pushscreen from phonelistener

Hey Thanks a lot Mr. Naveen..
your code helps me a lot..

Take care :smileyhappy:
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Unable to pushscreen from phonelistener

 


klyubin wrote:
To be more precise, immediately after you register the PhoneListener on startup, your application terminates (main() method returns). So, your listener has a reference to an application that is no longer running. Posting an invokeLater to the application won't achieve what you want because the application is no longer running (in particular, as simon_hain said, the event dispatcher is no longer processing events posted to the application).

 

while the practical difference is quite small i have to correct you here.

the phone listener has the phone application as the referenced uiapplication, and when you use getActiveScreen you get a reference to the PhoneScreen.

This also happens when you register the listener in your own UiApplication which is still alive.

 

You can give the phone listener a reference to your UiApp, or use runtimestore or global events to move the execution context to your instance.

It is a bit surprising, but also helpful if you want to meddle with the phone app :smileyhappy:

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter