Android Tutorials
Working With Android Contacts
Accessing Data With Android Cursors
Creating Lists Using The Android ListActivity
Android 9 Patch Scaled PNG Image Guide
Working With Images In Android
Exploring Android LinearLayout And RelativeLayout
Writing A Basic Android Application
Installing The Android SDK In Eclipse

Accessing Data With Android Cursors

Media Player

The Android Media-player also uses SQLite to store the media information. Use this database to find media stored on the device. Available fields are DATE_ADDED, DATE_MODIFIED, DISPLAY_NAME, MIME_TYPE, SIZE, and TITLE.

package higherpass.TestingData;

import android.app.Activity;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;
import android.widget.TextView;
import android.database.Cursor;

public class TestingData extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView view = (TextView) findViewById(R.id.hello);
        String[] projection = new String[] {
       		MediaStore.MediaColumns.DISPLAY_NAME
       		, MediaStore.MediaColumns.DATE_ADDED
       		, MediaStore.MediaColumns.MIME_TYPE
        };
        Cursor mCur = managedQuery(Media.EXTERNAL_CONTENT_URI,
       		projection, null, null, null
       		);
        mCur.moveToFirst();
 
        while (mCur.isAfterLast() == false) {
       	    for (int i=0; i<mCur.getColumnCount(); i++) {
         	view.append("n" + mCur.getString(i));
            }
            mCur.moveToNext();
        }
      
    }
}

The only differences here is that we use getColumnCount() to determine the number of columns in the returned records and loop through displaying each column.

Call Log

If granted permission Android applications can access the call log. The call log is accessed like other datastores.

package higherpass.TestingData; 

import android.app.Activity;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.widget.TextView;
import android.database.Cursor;

public class TestingData extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView view = (TextView) findViewById(R.id.hello);
        String[] projection = new String[] {
        		Calls.DATE
        		, Calls.NUMBER
        		, Calls.DURATION
        };
       Cursor mCur = managedQuery(CallLog.Calls.CONTENT_URI,
         		projection, Calls.DURATION +"<?", 
                        new String[] {"60"},
                        Calls.DURATION + " ASC");
        mCur.moveToFirst();

        while (mCur.isAfterLast() == false) {
              for (int i=0; i<mCur.getColumnCount(); i++) {
                  view.append("n" + mCur.getString(i));
              }
      	      mCur.moveToNext();
        }
      
    }
}

This final example introduces the rest of the managedQuery() parameters. After the projection we pass a WHERE clause in the same way they were crafted in the update and delete sections earlier. After the where clause comes the array of replacement arguments. Finally we pass in an order by clause to tell SQLite how to sort the results. This query only retrieves records for phone calls lasting less than 60 seconds and sorts them by duration ascending.

Transactions, Managed Queries, And Accessing Bookmarks <<  1 2 3 4
New Content