13 Use content providers and permissions by implementing read phonebook contacts with content providers and display in the list. res->layout->activity_contact_display.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listView" ></ListView> </RelativeLayout> ContactDisplay.java package com.example.contactdisplay; import android.os.Bundle; import android.provider.Contacts.People; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.PhoneLookup; import android.app.Activity; import android.database.Cursor; import android.view.Menu; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class ContactDisplay extends Activity { ListView list = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact_display); list=(ListView)findViewById(R.id.listView); Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, null, null,null, null); String contact = "Name Number"+"\n"; while(people.moveToNext() && cursor.moveToNext()) { int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME); contact = contact + people.getString(nameFieldColumnIndex); contact = contact+" --- " + cursor.getString(cursor.getColumnIndex(Phone.NUMBER))+"\n"; } String final_array[] = contact.split("\n"); ArrayAdapter<String> array = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,final_array); list.setAdapter(array); } } 14 Create an application to call a phone number entered by the user the Edit Text. strings.xml <resources> <string name="app_name">CallingDemo</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_calling_demo">CallingDemo</string> <string name="lblNum">Enter Mobile Number</string> <string name="call">CALL</string> </resources> res->layout->activity_calling_demo.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/lblNum" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txt" android:inputType="number" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn" android:text="@string/call" /> </LinearLayout> CallingDemo.java package com.example.callingdemo; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.widget.Button; public class CallingDemo extends Activity implements View.OnClickListener{ Button btn = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calling_demo); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(this); } public void onClick(View v) { Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:0377778888")); startActivity(callIntent); } } 15 Create an application that will create database to store username and password. res->layout-> activity_database_demo.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > </RelativeLayout> DatabaseDemo.java package com.example.database12exercise; import android.os.Bundle; import android.widget.Toast; import android.app.Activity; import android.database.sqlite.SQLiteDatabase; public class DatabaseDemo extends Activity { SQLHelper helper = null; SQLiteDatabase database = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_database_demo); helper = new SQLHelper(this); database = helper.getWritableDatabase(); String userName = "abc"; String password = "123"; String query = "insert into user_login values('"+userName+"','"+password+"');"; database.execSQL(query); Toast.makeText(this,"Record Insert Successfully...!!!", Toast.LENGTH_LONG).show(); } } SQLHelper.java package com.example.database12exercise; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class SQLHelper extends SQLiteOpenHelper static final String DATABASE_NAME = "login.db"; static final int DATABASE_VERSION = 1; public SQLHelper(Context context) { super(context,DATABASE_NAME,null,DATABASE_VERSION); } public void onCreate(SQLiteDatabase db) { String query = "create table user_login(usernme varchar(20), password varchar(30))"; db.execSQL(query); } public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { } } 16 Create an application to insert, update and delete a record from the database. res->values->strings.xml <resources> <string name="app_name">SQLHelperDemo</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">MainActivity</string> <string name="enter_no">Enter Number</string> <string name="enter_name">Enter Name</string> <string name="btn_add">ADD</string> <string name="btn_delete">DELETE</string> <string name="update">UPDATE</string> <string name="btn_clear">CLEAR ALL RECORDS</string> <string name="btnUpdate">UPDATE</string> </resources> res->layout->activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/enter_no" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtNum" android:inputType="number" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/enter_name" /> <EditText android:layout_width="match_parent” android:layout_height="wrap_content" android:id="@+id/txtName" android:inputType="text" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnAdd" android:text="@string/btn_add" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnDelete" android:text="@string/btn_delete" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnUpdate" android:text="@string/btnUpdate" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnClr" android:text="@string/btn_clear" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/showRecord" android:inputType="textMultiLine" android:lines="10" /> </LinearLayout> SQLHelper.java package com.example.sqlhelperdemo; mport android.content.Context; mport android.database.sqlite.SQLiteDatabase; mport android.database.sqlite.SQLiteOpenHelper; public class SQLHelper extends SQLiteOpenHelper static final String DATABASE_NAME = "student.db"; static final int DATABASE_VERSION = 1; public SQLHelper(Context context) { super(context,DATABASE_NAME,null,DATABASE_VERSION); } public void onCreate(SQLiteDatabase db) { String query = "create table std_info(no integer(5), name varchar(30))"; db.execSQL(query); } public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { } } MainActivity.java package com.example.sqlhelperdemo; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class MainActivity extends Activity implements View.OnClickListener{ SQLHelper helper = null; SQLiteDatabase database = null; String final_str = null; Cursor c = null; Button btnAdd = null; Button btnDelete = null; Button btnUpdate = null; Button btnClear = null; EditText txtNo = null; EditText txtName = null; EditText txtShow = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main) btnAdd = (Button)findViewById(R.id.btnAdd); btnDelete = (Button)findViewById(R.id.btnDelete); btnUpdate = (Button)findViewById(R.id.btnUpdate); btnClear = (Button)findViewById(R.id.btnClr); btnAdd.setOnClickListener(this); btnDelete.setOnClickListener(this); btnUpdate.setOnClickListener(this); btnClear.setOnClickListener(this); txtNo = (EditText)findViewById(R.id.txtNum); txtName = (EditText)findViewById(R.id.txtName); txtShow = (EditText)findViewById(R.id.showRecord); helper = new SQLHelper(this); database = helper.getWritableDatabase(); } public void onClick(View v) { if(v.getId()==R.id.btnAdd){ addRecord(); clearFields(); showRecord(); }else if(v.getId()==R.id.btnDelete){ deleteRecord(); clearFields(); showRecord(); }else if(v.getId()==R.id.btnUpdate){ updateRecord(); clearFields(); showRecord(); }else if(v.getId()==R.id.btnClr){ clearAllRecord(); txtShow.setText(""); } } ////////////// function for add records ////////////////// public void addRecord(){ String no = txtNo.getText().toString(); String name = txtName.getText().toString(); int number = Integer.parseInt(no); String query = "insert into std_info values("+number+",'"+name+"');"; database.execSQL(query); Toast.makeText(this,"Record Insert Successfully...!!!", Toast.LENGTH_LONG).show() } ////////////// function for delete records ////////////////// public void deleteRecord(){ String no = txtNo.getText().toString(); int number = Integer.parseInt(no); database.execSQL("delete from std_info where no = "+number); Toast.makeText(this,"Record Delete Successfully...!!!", Toast.LENGTH_LONG).show(); } ////////////// function for update records ////////////////// public void updateRecord(){ String no = txtNo.getText().toString(); int number = Integer.parseInt(no); String name = txtName.getText().toString(); database.execSQL("update std_info set name='"+name+"' where no = "+number); Toast.makeText(this,"Record Update Successfully...!!!", Toast.LENGTH_LONG). show(); } ////////////// function for show records in text area ////////// public void showRecord(){ final_str = "NUMBER NAME \n"; c = database.rawQuery("select * from std_info",null); if(c!=null){ int no_index = c.getColumnIndex("no"); int name_index = c.getColumnIndex("name"); c.moveToFirst(); String std_no; String std_name; do{ std_no = c.getString(no_index); std_name = c.getString(name_index); final_str = final_str + std_no + " "+std_name+"\n"; }while(c.moveToNext()); } txtShow.setText(final_str); } ////////////// function for clear all records ////////////////// public void clearAllRecord(){ database.execSQL("delete from std_info"); Toast.makeText(this,"All Records Are Clear...!!!", Toast.LENGTH_LONG).show(); } ////////////// function for clear fields ////////////////// public void clearFields(){ txtNo.setText(""); txtName.setText(""); } }