InsertActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.ContentProviders.ContactsListInsertContacts; import course.examples.ContentProviders.ContactsListWithInsDel.R; import android.accounts.AccountManager; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class InsertActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Exit if there are no google accounts if (AccountManager.get(this).getAccountsByType("com.google").length == 0) finish(); setContentView(R.layout.main); Button insertButton = (Button) findViewById(R.id.insert); insertButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Start the DisplayActivity startActivity(new Intent(InsertActivity.this, DisplayActivity.class)); } }); } } |
DisplayActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
package com.ContentProviders.ContactsListInsertContacts; import java.util.ArrayList; import java.util.List; import android.accounts.Account; import android.accounts.AccountManager; import android.app.ListActivity; import android.app.LoaderManager; import android.content.ContentProviderOperation; import android.content.CursorLoader; import android.content.Loader; import android.content.OperationApplicationException; import android.database.Cursor; import android.os.Bundle; import android.os.RemoteException; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.StructuredName; import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Data; import android.provider.ContactsContract.RawContacts; import android.util.Log; import android.widget.SimpleCursorAdapter; import course.examples.ContentProviders.ContactsListWithInsDel.R; public class DisplayActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> { public final static String[] mNames = new String[] { "Android Painter", "Steve Ballmer", "Steve Jobs", "Larry Page" }; private static final String columnsToExtract[] = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.STARRED }; private static final String columnsToDisplay[] = new String[] { Contacts.DISPLAY_NAME }; private static final int[] resourceIds = new int[] { R.id.name }; private static final String TAG = "ContactsListDisplayActivity"; private Account[] mAccountList; private String mType; private String mName; private SimpleCursorAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get Account information // Must have a Google account set up on your device mAccountList = AccountManager.get(this).getAccountsByType("com.google"); mType = mAccountList[0].type; mName = mAccountList[0].name; // Insert new contacts insertAllNewContacts(); // Create and set empty list adapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_layout, null, columnsToDisplay, resourceIds, 0); setListAdapter(mAdapter); // Initialize a CursorLoader getLoaderManager().initLoader(0, null, this); } // Insert all new contacts into Contacts ContentProvider private void insertAllNewContacts() { // Set up a batch operation on Contacts ContentProvider ArrayList<ContentProviderOperation> batchOperation = new ArrayList<ContentProviderOperation>(); for (String name : mNames) { addRecordToBatchInsertOperation(name, batchOperation); } try { // Apply all batched operations getContentResolver().applyBatch(ContactsContract.AUTHORITY, batchOperation); } catch (RemoteException e) { Log.i(TAG, "RemoteException"); } catch (OperationApplicationException e) { Log.i(TAG, "RemoteException"); } } // Insert named contact into Contacts ContentProvider private void addRecordToBatchInsertOperation(String name, List<ContentProviderOperation> ops) { int position = ops.size(); // First part of operation ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) .withValue(RawContacts.ACCOUNT_TYPE, mType) .withValue(RawContacts.ACCOUNT_NAME, mName) .withValue(Contacts.STARRED, 1).build()); // Second part of operation ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValueBackReference(Data.RAW_CONTACT_ID, position) .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) .withValue(StructuredName.DISPLAY_NAME, name).build()); } // Remove all newly-added contacts when activity is destroyed @Override protected void onDestroy() { deleteAllNewContacts(); super.onDestroy(); } private void deleteAllNewContacts() { for (String name : mNames) { deleteContact(name); } } private void deleteContact(String name) { getContentResolver().delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.Contacts.DISPLAY_NAME + "=?", new String[] { name }); } public Loader<Cursor> onCreateLoader(int id, Bundle args) { String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.DISPLAY_NAME + " != '' ) AND (" + Contacts.STARRED + "== 1))"; return new CursorLoader(this, Contacts.CONTENT_URI, columnsToExtract, select, null, Contacts._ID + " ASC"); } public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mAdapter.swapCursor(data); } public void onLoaderReset(Loader<Cursor> loader) { mAdapter.swapCursor(null); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/insert" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="200dip" android:text="@string/insert_contacts_string" android:textSize="24sp"> </Button> </RelativeLayout> |
list_layout.xml
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textSize="24sp" > </TextView> |