Search Wikipedia
Search results
May 2, 2014
May 1, 2014
Implement Android MP3 Player using MediaPlayer
MediaPlayer in Android
MediaPlayer class can be used to control playback of audio/video files and streams. It is a example to implement MP3 Player using MediaPlayer. Please note that you have to keep follow the State Diagram, otherwise IIlegalStateException will be thrown.
Create a new Android Project in Eclipse, using the auto-generated code extend from ActionBarActivity.
The mp3 file is stored in /res/raw/vespers.mp3. Filename match with the following code in initMediaPlayer().
The mp3 file is stored in /res/raw/vespers.mp3. Filename match with the following code in initMediaPlayer().
Labels:
MediaPlayer
Working With Android Contacts (Part 2)
Gluing together (API 2.0 and API 1.6 & Before)
To put this together into an application there are a few glue pieces that need to be setup along with creating classes to manage accessing the data. First we need to create a set of classes to hold the data. Also we'll create a class to handle 2.0 API calls and a class to handle 1.6 and earlier API calls. There's also a wrapper class that determines and loads the proper class.
Contact Data Classes
The contact classes are a series of classes to hold a list of contacts. The list is stored in the class ContactList that maintains an ArrayList of Contacts. The Contact objects are represented in the Contact class. The contact class stores all the data from the Android contact record. In addition to the ContactList and Contact classes there are specialized classes to represent some of the record data.
The contact classes are a series of classes to hold a list of contacts. The list is stored in the class ContactList that maintains an ArrayList of Contacts. The Contact objects are represented in the Contact class. The contact class stores all the data from the Android contact record. In addition to the ContactList and Contact classes there are specialized classes to represent some of the record data.
We will create classes to represent the address and email. Most of these classes are mere data storage classes with variables and getter/setters.
ContactList
The ContactList class is a very basic class designed to hold an ArrayList of instances of the Contact class below. We've left this class very plain and ready to be expanded to suit your needs.
package com.kais.android.ContactAPI.objects; import java.util.ArrayList; public class ContactList { private ArrayList<Contact> contacts = new ArrayList<Contact>(); public ArrayList<Contact> getContacts() { return contacts; } public void setContacts(ArrayList<Contact> contacts) { this.contacts = contacts; } public void addContact(Contact contact) { this.contacts.add(contact); } public ContactList() { } }
Working With Android Contacts (Part 1)
Introduction To Android Contacts
Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. See the Android SQLite and Cursor Article for more information. Google changed the contacts database moving from 1.x to 2.0 versions of Android. This tutorial will be broken into 2 sections. First covering accessing contacts in Android 2.0 and Android 1.6 & before. Third we'll glue it all together with a class that abstracts specific classes for each version and a set of classes to manage the data from the contact records for that tutorial click here.
API For 1.6 and Before
Granting Access
Before an application can query the contact records access must be granted through the AndroidManifest.xml file stored in the root of the project. Add the following uses-permission belows the uses-sdk statement.
<uses-permission android:name="android.permission.READ_CONTACTS" />
Querying the contact databaseRetrieving Contact DetailsBasic contact information stored in Contacts table with detailed information stored in individual tables for normalization. In Android 1.x to query the base contact records the URI to query is stored in People.CONTENT_URI.package com.kais.TestContacts; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.os.Bundle; import android.provider.Contacts; import android.provider.Contacts.People; public class TestContacts extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ContentResolver cr = getContentResolver(); Cursor cur = cr.query(People.CONTENT_URI, null, null, null, null); if (cur.getCount() > 0) { while (cur.moveToNext()) { String id = cur.getString(cur.getColumnIndex(People._ID)); String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME)); } } } }
Start off with the standard view loading. Then we create a ContentResolver instance that will be used to query the SQLite database that stores the contacts. The ContentResolver query returns a Cursor instance that holds the contact records queried from the database. Then take the ID field from the contact record and store it in the string id and take the DISPLAY_NAME field and place it in the string name. For more information about cursors see the Android Cursor Tutorial.
Labels:
Android Contacts
Accessing Data with Android Cursors (Part 2)
Retrieving Data
Retrieving data from SQLite databases in Android is done using Cursors. The Android SQLite query method returns a Cursor object containing the results of the query. To use Cursors android.database.Cursor must be imported.
About Cursor
Cursors store query result records in rows and grant many methods to access and iterate through the records. Cursors should be closed when no longer used, and will be deactivated with a call to Cursor.deactivate() when the application pauses or exists. On resume the Cursor.requery() statement is executed to re-enable the Cursor with fresh data. These functions can be managed by the parent Activity by calling startManagingCursor().
package com.kais.TestingData; import java.util.Locale; import android.app.Activity; import android.os.Bundle; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; 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); SQLiteDatabase db; db = openOrCreateDatabase( "TestingData.db" , SQLiteDatabase.CREATE_IF_NECESSARY , null ); db.setVersion(1); db.setLocale(Locale.getDefault()); db.setLockingEnabled(true); Cursor cur = db.query("tbl_countries", null, null, null, null, null, null); cur.close(); } }
Open the database as before. The query performed will return all records from the table tbl_countries. Next we'll look at how to access the data returned.
Apr 30, 2014
Accessing Data with Android Cursors (Part 1)
Creating and connecting to a Database
Create a new project in Eclipse called TestingData. Import android.database.sqllite.SQLiteDatabase in this project.
package com.kais.testingdata;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//here we create a database called TestingData.db
SQLiteDatabase db;
db = openOrCreateDatabase("TestingData.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
}
Creating Tables
Tables are created by executing statements on the database. The queries should be executed with the execSQL statement.Use the setVersion(), setLocale() and setLockingEnabled() methods to set these properties. These will be demonstrated in the creating tables example.
Just put these lines of codes below the creation of database.
final String CREATE_TABLE_COUNTRIES = "CREATE TABLE tbl_countries("+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"+"country_name TEXT);";
final String CREATE_TABLE_STATES = "CREATE_TABLE tbl_states("+" id INTEGER PRIMARY KEY AUTOINCREMENT,"+" state_name TEXT "+" country_id INTEGER KEY NULL CONSTRAINT "+" country_id INTEGER NOT NULL CONSRAINT "+" country_id REFRENCES tbl1_countries(id) "+" ON DELETE CASCADE);";
db.execSQL(CREATE_TABLE_COUNTRIES);
db.execSQL(CREATE_TABLE_STATES);
final String CREATE_TRIGGER_STATES =
"CREATE TRIGGER fk_insert_state BEFORE "
+ "INSERT on tbl_states"
+ "FOR EACH ROW "
+ "BEGIN "
+ "SELECT RAISE(ROLLBACK, 'insert on table
"+" tbl_states violates foreign key constraint
"+" fk_insert_state') WHERE (SELECT id FROM "+" tbl_countries WHERE id = NEW.country_id) IS NULL; "+" END;";
db.execSQL(CREATE_TRIGGER_STATES);
Additionally here we manually have to create triggers to handle the foreign key relationships between the table. In a production application there would also need to be foreign key triggers to handle row updates and deletes. The foreign key triggers are executed with execSQL just like the table creation.
Inserting Records
Android comes with a series of classes that simplify database usage. Use a ContentValues instance to create a series of table field to data matchings that will be passed into an insert() method. Android has created similar methods for updating and deleting records.
ContentValues values = new ContentValues();
values.put("country_name", "US");
long countryId = db.insert("tbl_countries", null, values);
ContentValues stateValues = new ContentValues();
stateValues.put("state_name", "Texas");
stateValues.put("country_id", Long.toString(countryId));
try {
db.insertOrThrow("tbl_states", null, stateValues);
} catch (Exception e) {
//catch code
}
Append this code to the previous example. First create a ContentValues object to store the data to insert and use the put method to load the data. Then use the insert() method to perform the insert query into SQLite. The insert() function expects three parameters, the table name, null, and the ContentValues pairs. Also a long is returned by the insert() function. This long will hold the primary key of the inserted row.
Mar 27, 2014
Using Google Maps V2 in Android
1. Downloading Google Play Services
Google made new Maps V2 API as a part of Google Play Services SDK. So before we start developing maps we need to download google play services from SDK manger. You can open SDK manager either from Eclipse or from android sdk folder.
Open Eclipse ⇒ Windows ⇒ Android SDK Manager and check whether you have already downloaded Google Play Services or not under Extras section. If not select play services and install the package.
![]() |
Downloading Google Play Services |
2.Importing Google Play Services in Eclipse
After downloading play services we need to import it to Eclipse which will be used as a library for our maps project.
1. In Eclipse goto File ⇒ Import ⇒ Android ⇒ Existing Android Code Into Workspace
2. Click on Browse and select Google Play Services project from your android sdk folder. You can locate play services library project from
android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib
android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib
3. Importantly while importing check Copy projects into workspace option as shown in the below image.
![]() |
Importing Google Play Services |
Labels:
Google Maps V2
Subscribe to:
Posts (Atom)