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
New Developer
cesarolea
Posts: 18
Registered: ‎11-20-2008
My Device: Not Specified
Accepted Solution

Compare dialed number to predefined list

I'm trying to get the currently dialed number and, based on a predefined list, do some actions. In order to do so, I've set up a custom PhoneListener and use the following code:

 

 

//get the dialed number
String phoneNumber = Phone.getActiveCall().getDisplayPhoneNumber();
//get predefined number list, as Integers
Vector numbers = Options.getContactNumbers();
int dialedNumber = 0;

//Convert the dialed number to int
try{
    dialedNumber = Integer.parseInt(phoneNumber);
}catch(Exception e){
    Dialog.alert("Disabled!");
    return false;
}

//loop the predefined number list
for(int i=0;i<numbers.size();i++){
    int currentNumber = ((Integer)numbers.elementAt(i)).intValue();
    if(currentNumber == dialedNumber){
        Dialog.alert("ENABLED!");
        return true;
    }
}

 The code above always displays the Disabled! dialog. I don't know why but it seems to throw an exception when parsing the string as Int value. Phone.getActiveCall().getDisplayPhoneNumber() retrieves the dialed number correctly, so I don't know why it fails when parsing it.

 

Is there a better way to do what I'm trying to do?

 

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Compare dialed number to predefined list

I don't think parseInt() is the way to go here. An int can only hold 2^31 value, and I believe a phone number can exceed this, especially with the country code appended.

 

Secondly, if there are any formatting characters (blanks, dashes, periods, etc) in the number, this will throw an exception.

 

My suggestion is to write a routine that normalizes the phone numbers by removing all foramtting characters, then use a string comparizon function for the search, OR turn the number into a long and compare that.

 

 

 

Please use plain text.
New Developer
cesarolea
Posts: 18
Registered: ‎11-20-2008
My Device: Not Specified

Re: Compare dialed number to predefined list

Thanks for the response!

 

I realize the number may contain other characters that make it throw an exception, but for testing purposes right now I'm using '123' as dialed number in the simulator, still doesn't work.

 

Originally I tried to do this with Strings, storing the list number as a String and avoiding conversion:

 

 

//get the dialed number
String phoneNumber = Phone.getActiveCall().getDisplayPhoneNumber();
//get predefined number list, as String
Vector numbers = Options.getContactNumbers();

//loop the predefined number list
for(int i=0;i<numbers.size();i++){
    if(((String)numbers.elementAt(i)).equals(phoneNumber)){
        Dialog.alert("ENABLED!");
        return true;
    }
}

 But the if never evaluated to true. I may have missed something, so if you can confirm that on a basic level (like using "123" as a dialed and stored number) it should work, that would put me back on track.

 

Please use plain text.
Developer
pfiffi78
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: Compare dialed number to predefined list

I suggest to put a breakpoint in and look at the string you get as displayed phone number and and also at the numbers you have stored in your vector in the Debugger. That should tell you why the equals() fails. 

 

Generally when comparing Strings it's a good idea to call trim() on the String you are not sure about how it will be formatted.

 

 

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Compare dialed number to predefined list

One other thing you should be aware of: if you are performing this comparison in the context of the phone listener, your Dialog.alert() will fail, since you are not in your applications event thread.

 

Change this to a System.out.println().

 

 

Please use plain text.
New Developer
cesarolea
Posts: 18
Registered: ‎11-20-2008
My Device: Not Specified

Re: Compare dialed number to predefined list

I am performing the comparison in the context of the phone listener, however the Dialog alert is displayed correctly. The reason I use an alert instead of System.out is that I can't get the printed output using bb-ant-tools. If you have any info on that, I appreciate it.

Please use plain text.
Developer
pfiffi78
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: Compare dialed number to predefined list

What have bb-ant-tools to do with System.out.println()? They are simply build tools and don't execute your code.

 

You won't see any output of System.out. println() on a real device and on the Simulator if you don't run it in the debugger.

 

So run your simulator in debug mode and you will see the output of System.out.println() and you will be able to see the values of the variables in your method and find out why the equals method fails.

 

It's quite easy to use and you should be able to spot the reason fairly instantly.

 

 

Please use plain text.
New Developer
cesarolea
Posts: 18
Registered: ‎11-20-2008
My Device: Not Specified

Re: Compare dialed number to predefined list

What I meant is that I use bb-ant-tools instead of the Eclipse plugin to build, run the simulator, sign the executables, etc. My problem was that I was running the simulator without the debugger apparently. Now I get System.out.println() messages.

 

Thanks pfifi78!

Please use plain text.