net_whiz
net_whiz

Reputation: 135

How to sort call logs to international calls and local calls?

I am in the phase of developing an application on call logs. Before i dive into it I would want to get some information on this as I have read through the call logs class in Android Developer.

I want to know if there is a way that I can sort the call logs and sms and extract only international and local(Also the same with sms). Is it also possible that I accomplish this task by using third party Library like the Libphonenumber? If anyone has any code and suggestion that will help me I will be most grateful. I would appreciate a solution in the form of code or an advice on how i can accomplish this. I really do not want to get stuck half way when i start working on the project

This is the code I have to get the call logs for only outgoing calls

    managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);
    int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER ); 
    int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
    int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);


    while ( managedCursor.moveToNext() ) {

    String phNumber = managedCursor.getString( number );

    String callType = managedCursor.getString( type );
    String callDate = managedCursor.getString( date );
    Date callDayTime = new Date(Long.valueOf(callDate));
    String callDuration = managedCursor.getString( duration );

    int realdura=Integer.parseInt(callDuration);

    String dir = null;
    int dircode = Integer.parseInt( callType );
    if(dircode==CallLog.Calls.OUTGOING_TYPE){
        dir="OutGoing Call";

    total_duration=realdura+total_duration;
    callList=("\nPhone Number:--- "+phNumber +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \nCall duration in sec :--- "+callDuration);

    data.add(callList);
    }
    }

    managedCursor.close();

    call.setText("the total duration is: "+total_duration);
    list_data=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
    call_list.setAdapter(list_data);
    }

Upvotes: 0

Views: 692

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

Using libphonenumber would seem to be a good idea to make things work internationally, there's a lot of country specifics in how they are represented.

What you need is the call PhoneNumberUtil.parse (which I can't seem to link directly to), and pick the country_code out of the resulting PhoneNumber (documented here in the example) That should allow you to just compare the country code to your local country code and easily know whether the call is international.

Here's a quick and dirty example (without error handling), "SE" is Sweden with international calling code "00" and country code "46" (ie the number is domestic and outputs that)

public static void main(String[] argv) throws NumberParseException {
    String str = "00468328999298";

    PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();

    Phonenumber.PhoneNumber number = phoneNumberUtil.parse(str, "SE");
    if(number.getCountryCode() != phoneNumberUtil.getCountryCodeForRegion("SE"))
        System.out.println("International");
    else
        System.out.println("Domestic");
}

Upvotes: 1

Related Questions