بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
گرفتن یک شماره از لیست مخاطب و نمایش آن ( حل نشده )
من یک اکتیویتی دارم که شامل یک دکمه و یک تسکت هست
میخوام زمانی که روی دکمه میزنم لیست مخاطبینم باز بشن و یک نفر رو انتخاب کنم و شمارش رو توی تکست نشون بدم
ممنون میشم راهنمایی کنید
0
0
لینک - با جستجوی : pick contact number android (11 سال پیش)
0
0
سرچ که کردم یه سری کد هم پیدا کردم اما هیچ کدوم جواب نمیدن تا انتخاب مخاطب میاد ولی شماره رو نشون نمیده (11 سال پیش)
0
0
این کد رو تست کردم فقط یه مجوز خوندن مخاطب میخواد که جالب نیست (11 سال پیش)
0
0
آره مجوز رو باید بهش داد ، منظورت از این که زیاد جالب نیست یعنی چی ؟ یعنی کد درستی نیست؟ (11 سال پیش)
+1
0
یعنی به خاطر گرفتن یه شماره نیاز برنامه باید مجوز داشته باشه که برای کاربر خوشایند نیست (البته برای فقط اونایی که دانشی در این مورد دارن) ، همچنین متد managedQuery منسوخ شده - جوابی که پایین گذاشتم نیاز به مجوز نداره :) (11 سال پیش)
برای این سوال 5 پاسخ وجود دارد.
پاسخ به سوال
سپهر
11 سال پیش
+1
0
از این کد ها استفاده کردم اما جواب نمیده
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 3);
}
});
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex("data1"));
System.out.println("number is:" + cNumber);
}
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
edtnumber.setText(name);
}
}
break;
}
}
پاسخ به سوال
Spirit
11 سال پیش
+4
0
ضرب المثل داریم آب در کوزه و ما گرد جهان میگردیم ، توی خود مستندات اندروید یه مثال توپ و تر و تمیز داده :
developer.android.com/training/basics/intents/result.html
درخواست :
static final int PICK_CONTACT_REQUEST = 1; // The request code
.
.
.
private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
پردازش نتیجه :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = {Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
// Do something with the phone number...
}
}
}
پاسخ به سوال
سپهر
11 سال پیش
+1
0
public class ActivityFriendSendRequest extends Activity {
private TabHost myTabHost;
static final int PICK_CONTACT_REQUEST = 1; // The request code
EditText edtnumber;
ImageView imgadd;
String cNumber;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friend_sendrequest);
imgadd = (ImageView) findViewById(R.id.imgaddcontact);
edtnumber = (EditText) findViewById(R.id.edtnumber);
imgadd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
});
}
//code
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = { Phone.NUMBER };
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
edtnumber.setText(number);
// Do something with the phone number...
}
}
}
پاسخ به سوال
محمد مقصودی
11 سال پیش
0
0
در هر صورتی شما باید permition روی توی مانی فست قرار دهید. خودم هم با این مشکل روبرو بودم و تا permition نگذاشتم درست نشد. این هم کد:
<uses-permission android:name="android.permission.READ_CONTACTS" />
پاسخ به سوال
وحید
11 سال پیش
0
0
من هم همین کدها رو وارد می کنم اما چندتا اررور دارم.
قابل توجه که من این کدها رو در یک کلاس کاستوم ویو که دارم نوشتم یعنی extends شده از LeanierLayout که یک ویو هست.
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class CustomContact extends LinearLayout {
static final int PICK_CONTACT_REQUEST = 1;
public CustomContact(Context context) {
super(context);
initialize(context);
}
public CustomContact(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public void initialize(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_contact, this, true);
Button btnSelectContact = (Button) view.findViewById(R.id.btnSelectContact);
btnSelectContact.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
});
}
//code
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = { Phone.NUMBER };
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
G.edtNumberMobile.setText(number);
// Do something with the phone number...
}
}
}
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .