|
Android example source code file (ContactAdder.java)
The ContactAdder.java Android example source code
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.contactmanager;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AuthenticatorDescription;
import android.accounts.OnAccountsUpdateListener;
import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import java.util.ArrayList;
import java.util.Iterator;
public final class ContactAdder extends Activity implements OnAccountsUpdateListener
{
public static final String TAG = "ContactsAdder";
public static final String ACCOUNT_NAME =
"com.example.android.contactmanager.ContactsAdder.ACCOUNT_NAME";
public static final String ACCOUNT_TYPE =
"com.example.android.contactmanager.ContactsAdder.ACCOUNT_TYPE";
private ArrayList<AccountData> mAccounts;
private AccountAdapter mAccountAdapter;
private Spinner mAccountSpinner;
private EditText mContactEmailEditText;
private ArrayList<Integer> mContactEmailTypes;
private Spinner mContactEmailTypeSpinner;
private EditText mContactNameEditText;
private EditText mContactPhoneEditText;
private ArrayList<Integer> mContactPhoneTypes;
private Spinner mContactPhoneTypeSpinner;
private Button mContactSaveButton;
private AccountData mSelectedAccount;
/**
* Called when the activity is first created. Responsible for initializing the UI.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG, "Activity State: onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_adder);
// Obtain handles to UI objects
mAccountSpinner = (Spinner) findViewById(R.id.accountSpinner);
mContactNameEditText = (EditText) findViewById(R.id.contactNameEditText);
mContactPhoneEditText = (EditText) findViewById(R.id.contactPhoneEditText);
mContactEmailEditText = (EditText) findViewById(R.id.contactEmailEditText);
mContactPhoneTypeSpinner = (Spinner) findViewById(R.id.contactPhoneTypeSpinner);
mContactEmailTypeSpinner = (Spinner) findViewById(R.id.contactEmailTypeSpinner);
mContactSaveButton = (Button) findViewById(R.id.contactSaveButton);
// Prepare list of supported account types
// Note: Other types are available in ContactsContract.CommonDataKinds
// Also, be aware that type IDs differ between Phone and Email, and MUST be computed
// separately.
mContactPhoneTypes = new ArrayList<Integer>();
mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_HOME);
mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_OTHER);
mContactEmailTypes = new ArrayList<Integer>();
mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_HOME);
mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_WORK);
mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_MOBILE);
mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_OTHER);
// Prepare model for account spinner
mAccounts = new ArrayList<AccountData>();
mAccountAdapter = new AccountAdapter(this, mAccounts);
mAccountSpinner.setAdapter(mAccountAdapter);
// Populate list of account types for phone
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Iterator<Integer> iter;
iter = mContactPhoneTypes.iterator();
while (iter.hasNext()) {
adapter.add(ContactsContract.CommonDataKinds.Phone.getTypeLabel(
this.getResources(),
iter.next(),
getString(R.string.undefinedTypeLabel)).toString());
}
mContactPhoneTypeSpinner.setAdapter(adapter);
mContactPhoneTypeSpinner.setPrompt(getString(R.string.selectLabel));
// Populate list of account types for email
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
iter = mContactEmailTypes.iterator();
while (iter.hasNext()) {
adapter.add(ContactsContract.CommonDataKinds.Email.getTypeLabel(
this.getResources(),
iter.next(),
getString(R.string.undefinedTypeLabel)).toString());
}
mContactEmailTypeSpinner.setAdapter(adapter);
mContactEmailTypeSpinner.setPrompt(getString(R.string.selectLabel));
// Prepare the system account manager. On registering the listener below, we also ask for
// an initial callback to pre-populate the account list.
AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
// Register handlers for UI elements
mAccountSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long i) {
updateAccountSelection();
}
public void onNothingSelected(AdapterView<?> parent) {
// We don't need to worry about nothing being selected, since Spinners don't allow
// this.
}
});
mContactSaveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onSaveButtonClicked();
}
});
}
/**
* Actions for when the Save button is clicked. Creates a contact entry and terminates the
* activity.
*/
private void onSaveButtonClicked() {
Log.v(TAG, "Save button clicked");
createContactEntry();
finish();
}
/**
* Creates a contact entry from the current UI values in the account named by mSelectedAccount.
*/
protected void createContactEntry() {
// Get values from UI
String name = mContactNameEditText.getText().toString();
String phone = mContactPhoneEditText.getText().toString();
String email = mContactEmailEditText.getText().toString();
int phoneType = mContactPhoneTypes.get(
mContactPhoneTypeSpinner.getSelectedItemPosition());
int emailType = mContactEmailTypes.get(
mContactEmailTypeSpinner.getSelectedItemPosition());;
// Prepare contact creation request
//
// Note: We use RawContacts because this data must be associated with a particular account.
// The system will aggregate this with any other data for this contact and create a
// coresponding entry in the ContactsContract.Contacts provider for us.
ArrayList<ContentProviderOperation> ops = new ArrayList
Other Android examples (source code examples)Here is a short list of links related to this Android ContactAdder.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.