[VIDEO-Tut] - Querying and Displaying the CallLog

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[VIDEO-Tut] - Querying and Displaying the CallLog

Postby plusminus » Sat Dec 01, 2007 8:25 pm

[align=center][VIDEO-Tut] - Querying and Displaying the CallLog[/align]

What you learn: You will learn how to query for the CallLog using a Cursor and display all Incoming/Outgoing/Missed calls in an IconifiedList.

Difficulty: 1.5 of 5 :)

:idea: Read Before: Iconified List - The making of !

What it will look like:
[align=center] Filesize: 10 MBytes :!:
Image[/align]

The Full Source:
Do not forget:
:arrow: The little Icons for Incoming/Outgoing/Missed
:arrow: The source of the Iconified List :!:


AndroidManifest.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    package="org.anddev.android.calllogdisplayer">
  6.  
  7.     <uses-permission id="android.permission.READ_CONTACTS"/>
  8.  
  9.     <application android:icon="@drawable/icon">
  10.  
  11.         <activity class=".CallLogDisplayer" android:label="@string/app_name">
  12.  
  13.             <intent-filter>
  14.  
  15.                 <action android:value="android.intent.action.MAIN" />
  16.  
  17.                 <category android:value="android.intent.category.LAUNCHER" />
  18.  
  19.             </intent-filter>
  20.  
  21.         </activity>
  22.  
  23.     </application>
  24.  
  25. </manifest>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


/src/your_package_structure/CallLogDisplayer.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.calllogdisplayer;
  2.  
  3. import java.util.ArrayList;
  4.  
  5.  
  6.  
  7. import org.anddev.android.calllogdisplayer.iconifiedlist.IconifiedText;
  8.  
  9. import org.anddev.android.calllogdisplayer.iconifiedlist.IconifiedTextListAdapter;
  10.  
  11.  
  12.  
  13. import android.app.ListActivity;
  14.  
  15. import android.database.Cursor;
  16.  
  17. import android.graphics.drawable.Drawable;
  18.  
  19. import android.os.Bundle;
  20.  
  21. import android.util.DateUtils;
  22.  
  23.  
  24.  
  25. public class CallLogDisplayer extends ListActivity {
  26.  
  27.         /** Called when the activity is first created. */
  28.  
  29.         @Override
  30.  
  31.         public void onCreate(Bundle icicle) {
  32.  
  33.                 super.onCreate(icicle);
  34.  
  35.                
  36.  
  37.                 // Querying for a cursor is like querying for any SQL-Database
  38.  
  39.                 Cursor c = getContentResolver().query(
  40.  
  41.                                 android.provider.CallLog.Calls.CONTENT_URI,
  42.  
  43.                                 null, null, null,
  44.  
  45.                                 android.provider.CallLog.Calls.DATE + " DESC");
  46.  
  47.                 startManagingCursor(c);
  48.  
  49.                
  50.  
  51.                 // Retrieve the column-indixes of phoneNumber, date and calltype
  52.  
  53.                 int numberColumn = c.getColumnIndex(
  54.  
  55.                                 android.provider.CallLog.Calls.NUMBER);
  56.  
  57.                 int dateColumn = c.getColumnIndex(
  58.  
  59.                                 android.provider.CallLog.Calls.DATE);
  60.  
  61.                 // type can be: Incoming, Outgoing or Missed
  62.  
  63.                 int typeColumn = c.getColumnIndex(
  64.  
  65.                                 android.provider.CallLog.Calls.TYPE);
  66.  
  67.                
  68.  
  69.                 // Will hold the calls, available to the cursor
  70.  
  71.                 ArrayList<IconifiedText> callList = new ArrayList<IconifiedText>();
  72.  
  73.                
  74.  
  75.                 // Loop through all entries the cursor provides to us.
  76.  
  77.                 if(c.first()){
  78.  
  79.                         do{
  80.  
  81.                                 String callerPhoneNumber = c.getString(numberColumn);
  82.  
  83.                                 int callDate = c.getInt(dateColumn);
  84.  
  85.                                 int callType = c.getInt(typeColumn);
  86.  
  87.                                
  88.  
  89.                                 Drawable currentIcon = null;
  90.  
  91.                                 switch(callType){
  92.  
  93.                                         case android.provider.CallLog.Calls.INCOMING_TYPE:
  94.  
  95.                                                 currentIcon = getResources().getDrawable(R.drawable.in);
  96.  
  97.                                                 break;
  98.  
  99.                                         case android.provider.CallLog.Calls.MISSED_TYPE:
  100.  
  101.                                                 currentIcon = getResources().getDrawable(R.drawable.missed);
  102.  
  103.                                                 break;
  104.  
  105.                                         case android.provider.CallLog.Calls.OUTGOING_TYPE:
  106.  
  107.                                                 currentIcon = getResources().getDrawable(R.drawable.out);
  108.  
  109.                                                 break;
  110.  
  111.                                 }
  112.  
  113.                                 // Convert the unix-timestamp to a readable datestring
  114.  
  115.                                 String dateString = DateUtils.dateString(callDate).toString();
  116.  
  117.                                
  118.  
  119.                                 callList.add(new IconifiedText("@ " + dateString
  120.  
  121.                                                 + "  | # " + callerPhoneNumber, currentIcon));
  122.  
  123.                         }while(c.next());
  124.  
  125.                 }
  126.  
  127.                
  128.  
  129.                 // Create an ListAdapter that manages to display out 'callList'
  130.  
  131.                 IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this);
  132.  
  133.                 itla.setListItems(callList);
  134.  
  135.                 this.setListAdapter(itla);
  136.  
  137.                 // Done =)
  138.  
  139.         }
  140.  
  141. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Regards,
plusminus

PS: I could not figure out, why the calls happened in December. Someone has seen the default-date of the emulator :?:
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Need some help on the call log

Postby alonav » Fri Mar 07, 2008 6:15 pm

First of all that looks like a great example, but i can't make it work - i'm a bit new in java

line 49 in CallLogDisplayer class got an error:"R.drawable.in cannot be resolved"
line 52 in CallLogDisplayer class got an error:"R.drawable.missed cannot be resolved"
line 55 in CallLogDisplayer class got an error:"R.drawable.out cannot be resolved"

line 59 in IconifiedText class got an error: "The method compareTo(IconifiedText) of type IconifiedText must override a superclass method"

can i download the whole project from somewhere?

Please help!
THNX
alonav
Freshman
Freshman
 
Posts: 4
Joined: Fri Mar 07, 2008 5:42 pm

activity class does not exist?

Postby alonav » Sat Mar 08, 2008 12:02 am

After fixing my previouse reply errors (i got rid of the @override of the compareto function) i'm getting another error after trying to run the project:
ActivityManager: Error: Activity class {CallLogDisplayer/CallLogDisplayer.CallLogDisplayer} does not exist.

Please help
THNX
alonav
Freshman
Freshman
 
Posts: 4
Joined: Fri Mar 07, 2008 5:42 pm

Postby plusminus » Sat Mar 08, 2008 5:25 pm

Hello alonav,

btw: you should work with JDK 1.6 instead of 1.5 !

You need to adopt the AndroidManifest.xml-File:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. package="your.complete.package.path.to.the.activity">
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <activity class=".NAMEOFYOURACTIVITYHERE" android:label="@string/app_name">
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

ActivityManager: Error: Activity class {CallLogDisplayer/Cal

Postby alonav » Wed Mar 12, 2008 8:20 pm

Hi again,

I updated the JDK to 1.6 & configured it in eclipse

the problem remains the same:
my package in XML file is package="CallLogDisplayer">

I can't write "class" like you wrote - i have an error on that
<activity android:name=".CallLogDisplayer" android:label="@string/app_name">

i can't write android:value either
so i wrote:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

THNX
alonav
Freshman
Freshman
 
Posts: 4
Joined: Fri Mar 07, 2008 5:42 pm

can we initiate the recent call application?

Postby gzhhong » Mon May 19, 2008 2:05 pm

I think the most effiective way to show the missed calls is to initiate the recent call application of android.

There are a table named calls in the contacts database and the calllog is a content provider named content.provider.calllog.calls.

Maybe a effective way is like below:

Intent i = new Intent();
i.setAction(Intent.VIEW_ACTION);
i.setData(new ContentURI("content://xxxxxxxx"));
startActivity(i);

if we can provide the correct uri in setData, maybe we can show the missed call logs in the recent calls application, right?

The question is: what is the correct uri?
gzhhong
Freshman
Freshman
 
Posts: 7
Joined: Mon May 19, 2008 1:56 pm

Top

a quickly method to show

Postby gzhhong » Mon May 19, 2008 3:59 pm

Intent i = new Intent();
i.setAction(Intent.VIEW_ACTION);
i.setData(new ContentURI("content://call_log/calls"));
startActivity(i);
gzhhong
Freshman
Freshman
 
Posts: 7
Joined: Mon May 19, 2008 1:56 pm

how to list the missed call only

Postby gzhhong » Tue May 20, 2008 2:30 pm

Another question is how to filter the missed call log by the content uri?
gzhhong
Freshman
Freshman
 
Posts: 7
Joined: Mon May 19, 2008 1:56 pm

Re: [VIDEO-Tut] - Querying and Displaying the CallLog

Postby xsuo » Thu May 22, 2008 7:45 am

problem:

java.lang.SecurityException:
permission

android.permission.READ_CONTACTS required for provider
call_log.
Attachments
1.jpg
1.jpg (41.73 KiB) Viewed 93728 times
xsuo
Freshman
Freshman
 
Posts: 4
Joined: Wed May 21, 2008 2:58 am

How to apply ScrollView to your IconifiedList

Postby myandroid » Thu Jul 31, 2008 2:34 am

hello,plusminus
How to apply ScrollView to your IconifiedList
i try it like this in layout.xml and init the list in Activity's onCreate function
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</ScrollView>
But i got an error:list views can't have unspecified size
can you help me to see it,thanks
Do my best to do everything
User avatar
myandroid
Freshman
Freshman
 
Posts: 3
Joined: Tue Jul 08, 2008 6:32 am
Location: china

Call Log Action ???

Postby trushal » Tue Jan 13, 2009 1:16 pm

:( what is the <action> for Listening Call Log change event ??

i simply want to access call log in back end app ...

thank u in advance .......
TRUSHAL PATEL
Soft.Dev
trushal
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jan 09, 2009 10:06 am

Help

Postby iPaul Pro » Mon Mar 16, 2009 1:25 am

I've been stuck on this for hours. I'm new to Java and Android. I'd like to make an app that displays a list of missed calls (for personal purposes). I tried to implement this code but have had no luck. I even built the package with the example names. These are the errors I receive:

DateUtils cannot be resolved
R.drawable.in cannot be resolved
R.drawable.missed cannot be resolved
R.drawable.out cannot be resolved
The import android.util.DateUtils cannot be resolved
The method first() is undefined for the type Cursor
The method next() is undefined for the type Cursor
The method isSelectable(int) is undefined for the type BaseAdapter
The method compareTo(IconifiedText) of type IconifiedText must override a superclass method

I literally have all of the same code and files from this tutorial and the IconifiedText tutorial in my package.

Please help! Anyone
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Postby iPaul Pro » Tue Mar 17, 2009 3:44 pm

I really don't know what I'm doing, so I wouldn't suggest following my instruction unless verified by someone else:

I have removed all of the errors in Osborns code, and I can actually try out the app now (yay!), but alas, it does nothing. I'm going to learn to debug and report back. But in the mean time, figured I'd share how I got it 'working'.

Okay, so I removed the DateUtils stuff altogether. Couldn't figure it out.
My mistake - forgot to copy the icons to 'res/layout' (r.drawables fixed)
first() should be isFirst()
next() should be moveToNext()
Remove the "super" from in front of isSelectable (replace with "this")
remove @Override for IconifiedText
Last edited by iPaul Pro on Wed Mar 25, 2009 4:03 am, edited 1 time in total.
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Postby iPaul Pro » Mon Mar 23, 2009 1:37 pm

I got it working!

using all of my corrections from above, EXCEPT:

isFirst() should be moveToFirst()

Now, if anyone out there can a) give a hint as to how to further customize the views in the iconified list, or b) give an updated example with XML layout, that would be great.

Thanks
Last edited by iPaul Pro on Fri Apr 03, 2009 12:11 am, edited 1 time in total.
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Postby iPaul Pro » Fri Mar 27, 2009 3:15 am

android.util.DateUtils is deprecated.

Does anybody have a good solution to replace:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String dateString = DateUtils.dateString(callDate).toString();
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


I tried:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. long created = c.getLong(dateColumn);
  2.  
  3. Date date = new Date(created);
  4.  
  5. String dateString = DateUtils.formatDate(date);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


and

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. long created = c.getLong(dateColumn);
  2.  
  3. Date date = new Date(created);
  4.  
  5. String dateString = DateFormat.getDateTimeInstance().format(date);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


But this makes the app take a long time to load. This (loading) issue may be due to factors, other than using DateUtils and DateFormat - but I'm not sure. Any help or hints on speeding up Date parsing (or perhaps making it happen after the list fully loads) would be greatly appreciated.

Thanks.
iPaul Pro
Developer
Developer
 
Posts: 33
Joined: Sun Mar 15, 2009 9:01 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests