Hy all,
在我需要的APP中,在列表视图中显示联系人列表(目前还不错)。
我想要在数字右边的滑动中拨打APP号码。
我现在有这个:
MainActivity:
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentLayout, new FragContact()).commit();
在我的片段中:
public class FragContact extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS) == true) {
explain();
} else {
//demander l'autorisation
}
} else {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final ListView list = (ListView) rootView.findViewById(android.R.id.list);
final List<Map<String, Object>> contacts = retrieveContacts(getActivity().getContentResolver());
if (contacts != null) {
final SimpleAdapter adapter = new SimpleAdapter(getActivity(), contacts, R.layout.contact, new String[]{"contactNumber", "photo"}, new int[]{R.id.name,
R.id.photo});
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if ((view instanceof ImageView) & (data instanceof Bitmap)) {
final ImageView image = (ImageView) view;
final Bitmap photo = (Bitmap) data;
image.setImageBitmap(photo);
return true;
}
return false;
}
});
list.setAdapter(adapter);
}
return rootView;
}
return null;
} else {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final ListView list = (ListView) rootView.findViewById(android.R.id.list);
final List<Map<String, Object>> contacts = retrieveContacts(getActivity().getContentResolver());
if (contacts != null) {
final SimpleAdapter adapter = new SimpleAdapter(getActivity(), contacts, R.layout.contact, new String[]{"contactNumber", "photo"}, new int[]{R.id.name,
R.id.photo});
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if ((view instanceof ImageView) & (data instanceof Bitmap)) {
final ImageView image = (ImageView) view;
final Bitmap photo = (Bitmap) data;
image.setImageBitmap(photo);
return true;
}
return false;
}
});
list.setAdapter(adapter);
}
return rootView;
}
}
public List<Map<String, Object>> retrieveContacts(ContentResolver contentResolver)
{
final List<Map<String, Object>> contacts = new ArrayList<Map<String, Object>>();
final Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID}, null, null, null);
if (cursor == null)
{
Log.e("retrieveContacts", "Cannot retrieve the contacts");
return null;
}
if (cursor.moveToFirst() == true)
{
do {
final long id = Long.parseLong(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
final String contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (contactNumber != null) {
final Bitmap photo = getPhoto(contentResolver, id);
final Map<String, Object> contact = new HashMap<String, Object>();
contact.put("contactNumber", contactNumber);
contact.put("photo", photo);
contacts.add(contact);
}
}
while (cursor.moveToNext() == true);
}
if (cursor.isClosed() == false)
{
cursor.close();
}
return contacts;
}
private Bitmap getPhoto(ContentResolver contentResolver, long contactId)
{
Bitmap photo = null;
final Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
final Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
final Cursor cursor = contentResolver.query(photoUri, new String[] { ContactsContract.Contacts.Photo.DATA15 }, null, null, null);
if (cursor == null)
{
Log.e("getPhoto", "Cannot retrieve the photo of the contact with id '" + contactId + "'");
return null;
}
if (cursor.moveToFirst() == true)
{
final byte[] data = cursor.getBlob(0);
if (data != null)
{
photo = BitmapFactory.decodeStream(new ByteArrayInputStream(data));
}
}
if (cursor.isClosed() == false)
{
cursor.close();
}
return photo;
}
private void askForPermission()
{
requestPermissions(new String[] { Manifest.permission.READ_CONTACTS }, 2);
}
private void explain()
{
askForPermission();
}
}
Actualy我在那里:
感谢您的帮助。