this is a part of the app im creating which detects the drowsiness state of a person i.e., if a person's eyes are closed for more than 4 seconds then an alarm sound plays on the phone and a call will be initiated to the picked contact . But the call is not going. tested using logcat to see if the contact name and number is fetched correctly and the output is correct .
// Method to open the contacts list
public void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
mActivity.startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
// Constants for handling contact selection result
private static final int PICK_CONTACT_REQUEST = 1;
// Method to process the selected contact information
public void processContactSelection(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT_REQUEST && resultCode == Activity.RESULT_OK) {
Uri contactUri = data.getData();
Cursor cursor = null;
try {
cursor = mActivity.getContentResolver().query(contactUri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int nameColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
// Get the phone number from the ContactsContract.CommonDataKinds.Phone table
Cursor phoneCursor = mActivity.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null
);
if (phoneCursor != null && phoneCursor.moveToFirst()) {
int numberColumnIndex = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
mContactName = cursor.getString(nameColumnIndex);
mContactNumber = phoneCursor.getString(numberColumnIndex);
Log.d("EmergencyContactManager", "Contact Name: " + mContactName);
Log.d("EmergencyContactManager", "Contact Number: " + mContactNumber);
}
if (phoneCursor != null) {
phoneCursor.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
// Method to call the emergency contact
public void callEmergencyContact() {
String number = "tel:" + mContactNumber;
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));
mActivity.startActivity(callIntent);
}
// You can add getters and setters for the contact details if needed.
}```