/development/samples/ApiDemos/src/com/example/android/apis/content/PickContact.java

https://gitlab.com/brian0218/rk3188_rk3066_r-box_android4.4.2_sdk · Java · 114 lines · 78 code · 13 blank · 23 comment · 11 complexity · e272e2e29aad7809c329fcc4a95f14fa MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.android.apis.content;
  17. // Need the following import to get access to the app resources, since this
  18. // class is in a sub-package.
  19. import com.example.android.apis.R;
  20. import android.app.Activity;
  21. import android.app.AlarmManager;
  22. import android.app.PendingIntent;
  23. import android.content.Intent;
  24. import android.database.Cursor;
  25. import android.net.Uri;
  26. import android.os.SystemClock;
  27. import android.os.Bundle;
  28. import android.provider.BaseColumns;
  29. import android.provider.ContactsContract;
  30. import android.view.View;
  31. import android.view.View.OnClickListener;
  32. import android.widget.Button;
  33. import android.widget.Toast;
  34. import java.util.Calendar;
  35. /**
  36. * Demonstrates launching the contacts app to pick a contact. Does not
  37. * require permission to read contacts, as that permission will be granted
  38. * when the selected contact is returned.
  39. */
  40. public class PickContact extends Activity {
  41. Toast mToast;
  42. ResultDisplayer mPendingResult;
  43. class ResultDisplayer implements OnClickListener {
  44. String mMsg;
  45. String mMimeType;
  46. ResultDisplayer(String msg, String mimeType) {
  47. mMsg = msg;
  48. mMimeType = mimeType;
  49. }
  50. public void onClick(View v) {
  51. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  52. intent.setType(mMimeType);
  53. mPendingResult = this;
  54. startActivityForResult(intent, 1);
  55. }
  56. }
  57. @Override
  58. protected void onCreate(Bundle savedInstanceState) {
  59. super.onCreate(savedInstanceState);
  60. setContentView(R.layout.pick_contact);
  61. // Watch for button clicks.
  62. ((Button)findViewById(R.id.pick_contact)).setOnClickListener(
  63. new ResultDisplayer("Selected contact",
  64. ContactsContract.Contacts.CONTENT_ITEM_TYPE));
  65. ((Button)findViewById(R.id.pick_person)).setOnClickListener(
  66. new ResultDisplayer("Selected person",
  67. "vnd.android.cursor.item/person"));
  68. ((Button)findViewById(R.id.pick_phone)).setOnClickListener(
  69. new ResultDisplayer("Selected phone",
  70. ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE));
  71. ((Button)findViewById(R.id.pick_address)).setOnClickListener(
  72. new ResultDisplayer("Selected address",
  73. ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE));
  74. }
  75. @Override
  76. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  77. if (data != null) {
  78. Uri uri = data.getData();
  79. if (uri != null) {
  80. Cursor c = null;
  81. try {
  82. c = getContentResolver().query(uri, new String[] { BaseColumns._ID },
  83. null, null, null);
  84. if (c != null && c.moveToFirst()) {
  85. int id = c.getInt(0);
  86. if (mToast != null) {
  87. mToast.cancel();
  88. }
  89. String txt = mPendingResult.mMsg + ":\n" + uri + "\nid: " + id;
  90. mToast = Toast.makeText(this, txt, Toast.LENGTH_LONG);
  91. mToast.show();
  92. }
  93. } finally {
  94. if (c != null) {
  95. c.close();
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }