📌  相关文章
📜  如何通过Intent在Android中打开拨号程序?

📅  最后修改于: 2021-05-08 20:46:22             🧑  作者: Mango

电话拨号器是Android操作系统提供的一项呼叫号码的活动。通常,此类活动可能有也可能没有EditText(用于将数字作为输入)和Call(呼叫)按钮。当用户按下“呼叫”按钮时,它将调用拨号器应用程序活动。建议使用’tel:’前缀,否则将抛出Java.lang.IllegalStateException 。 Action_Dial不需要任何许可。以下是Java和Kotlin中MainActivity文件的代码。

Java
package com.geeksforgeeks.gfg.dial;
  
// importing packages
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
  
        // Binding MainActivity.java with 
        // activity_main.xml file
        setContentView(R.layout.activity_main);
      
    }
  
    // This function is called when button is clicked.
    public  void Call(View v)
    {
        // Find the EditText by its unique ID
        EditText e = (EditText)findViewById(R.id.editText);
  
        // show() method display the toast with message 
        // "clicked"
        Toast.makeText(this, "clicked", Toast.LENGTH_LONG)
             .show();
  
        // Use format with "tel:" and phoneNumber created is 
        // stored in u.
        Uri u = Uri.parse("tel:" + e.getText().toString());
  
        // Create the intent and set the data for the 
        // intent as the phone number.
        Intent i = new Intent(Intent.ACTION_DIAL, u);
  
        try
        {
            // Launch the Phone app's dialer with a phone 
            // number to dial a call.
            startActivity(i); 
        }
        catch (SecurityException s)
        {
            // show() method display the toast with 
            // exception message.
            Toast.makeText(this, "An error occurred", Toast.LENGTH_LONG)
                 .show();
        }
    }
}


Kotlin
package com.geeksforgeeks.gfg.dial
  
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.gfgreviewinjava.R
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
  
        // Binding MainActivity.java with
        // activity_main.xml file
        setContentView(R.layout.activity_main)
    }
  
    // This function is called when button is clicked.
    fun Call(v: View?) {
        // Find the EditText by its unique ID
        val e = findViewById(R.id.editText) as EditText
  
        // show() method display the toast with message
        // "clicked"
        Toast.makeText(this, "clicked", Toast.LENGTH_LONG)
                .show()
  
        // Use format with "tel:" and phoneNumber created is
        // stored in u.
        val u = Uri.parse("tel:" + e.text.toString())
  
        // Create the intent and set the data for the
        // intent as the phone number.
        val i = Intent(Intent.ACTION_DIAL, u)
        try {
            // Launch the Phone app's dialer with a phone
            // number to dial a call.
            startActivity(i)
        } catch (s: SecurityException) {
            // show() method display the toast with
            // exception message.
            Toast.makeText(this, "An error occurred", Toast.LENGTH_LONG)
                    .show()
        }
    }
}


XML


    android:layout_width="match_parent"
  
    
    android:layout_height="match_parent"
  
    tools:context="com.example.hp.dial.MainActivity">
  
    
        android:layout_width="wrap_content"
  
        
        android:layout_height="wrap_content"
  
        
        android:layout_marginLeft="8dp"
  
        
        android:layout_marginRight="8dp"
  
        
        android:layout_marginTop="65dp"
  
        
        android:hint="Phone No."
  
        
        android:inputType="phone"
  
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
    


为了进行直接呼叫而不切换到拨号程序活动,您需要添加Intent.ACTION_CALL代替Intent.ACTION_DIAL。
activity_main.xml

XML格式



    android:layout_width="match_parent"
  
    
    android:layout_height="match_parent"
  
    tools:context="com.example.hp.dial.MainActivity">
  
    
        android:layout_width="wrap_content"
  
        
        android:layout_height="wrap_content"
  
        
        android:layout_marginLeft="8dp"
  
        
        android:layout_marginRight="8dp"
  
        
        android:layout_marginTop="65dp"
  
        
        android:hint="Phone No."
  
        
        android:inputType="phone"
  
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
    

AndroidManifest.xml中,包括以下权限以直接调用而无需在拨号器中打开。要通过拨号器中的打开方式拨打电话,不需要以下权限。

uses-permission android:name="android.permission.CALL_PHONE"

图1.输入电话号码。

图2.打开Dialer活动。