एंड्रॉइड में फोन कॉल करना
यह गाइड संपर्कों से नंबर चुनकर या मैन्युअल रूप से दर्ज करके फोन कॉल शुरू करने को कवर करता है, जिसमें रनटाइम CALL_PHONE परमिशन को सही ढंग से संभाला जाता है।
अनुमतियां
<uses-permission android:name="android.permission.CALL_PHONE" /> <!-- संपर्क चुनने के लिए वैकल्पिक --> <uses-permission android:name="android.permission.READ_CONTACTS" />
दृष्टिकोण 1 - सीधा डायल (ACTION_DIAL) - परमिशन की आवश्यकता नहीं
डायलर को नंबर के साथ खोलता है; उपयोगकर्ता कॉल बटन दबाता है।
public void openDialer(String phoneNumber) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phoneNumber));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
दृष्टिकोण 2 - सीधा कॉल (ACTION_CALL) - CALL_PHONE परमिशन आवश्यक
उपयोगकर्ता इंटरैक्शन के बिना कॉल करता है। संवेदनशील — सावधानी से उपयोग करें।
private static final int REQUEST_CALL = 101;
public void makeCallDirect(String phoneNumber) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
== PackageManager.PERMISSION_GRANTED) {
placeCall(phoneNumber);
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL);
}
}
private void placeCall(String number) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] perms, @NonNull int[] results) {
super.onRequestPermissionsResult(requestCode, perms, results);
if (requestCode == REQUEST_CALL && results.length > 0 && results[0] == PackageManager.PERMISSION_GRANTED) {
placeCall(pendingNumber); // मेंबर फील्ड में संग्रहीत
}
}
दृष्टिकोण 3 - संपर्क से चुनें फिर कॉल करें
private final ActivityResultLauncherpickContact = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK && result.getData() != null) { Uri contactUri = result.getData().getData(); String number = extractPhoneNumber(contactUri); if (number != null) makeCallDirect(number); } }); private void launchContactPicker() { Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); pickContact.launch(intent); } private String extractPhoneNumber(Uri contactUri) { Cursor cursor = getContentResolver().query(contactUri, new String[]{ContactsContract.Contacts._ID}, null, null, null); if (cursor == null || !cursor.moveToFirst()) return null; long id = cursor.getLong(0); cursor.close(); Cursor pCur = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{String.valueOf(id)}, null); if (pCur != null && pCur.moveToFirst()) { String num = pCur.getString(0); pCur.close(); return num; } return null; }
UI उदाहरण
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp">
<EditText
android:id="@+id/etNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="फोन नंबर दर्ज करें"
android:inputType="phone" />
<Button
android:id="@+id/btnDial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="डायलर खोलें (सुरक्षित)" />
<Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="सीधे कॉल करें (परमिशन आवश्यक)" />
<Button
android:id="@+id/btnPickContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="संपर्क से चुनें और कॉल करें" />
<TextView
android:id="@+id/tvStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textColor="#666" />
</LinearLayout>
सुरक्षा और नीति नोट्स
- Google Play नीति:
CALL_PHONEका उपयोग करने वाले ऐप्स को प्रमुख कार्यक्षमता (डायलर, VoIP) की आवश्यकता होती है। अनावश्यक उपयोग से अस्वीकृति होती है। - आपातकालीन नंबर: कुछ क्षेत्र आपातकालीन नंबरों पर
ACTION_CALLको ब्लॉक करते हैं;ACTION_DIALसुरक्षित है। - दोहरी सिम: इंटेंट डिफ़ॉल्ट सिम का उपयोग करता है; उपयोगकर्ता डायलर UI में चुनता है।
मूल उदाहरण की समीक्षा
मूल कोड संपर्क क्वेरी, परमिशन लॉजिक और कॉल लॉजिक को एक विशाल गतिविधि में मिलाता था, Timer लीक करता था, और डिप्रिकेटेड startActivityForResult का उपयोग करता था। ऊपर दिए गए दृष्टिकोण मॉड्यूलर, परमिशन-सुरक्षित हैं, और आधुनिक Activity Result API का उपयोग करते हैं।