SoFunction
Updated on 2025-03-11

Android practical tutorial: Simple implementation of call function

This example shares the implementation code of Android calling function. You need a text input box to enter the number and a button to make a call.

Essence: Click the button to call the system's call function.

XML layout file code:

<LinearLayout xmlns:andro 
 xmlns:tools="/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context=".MainActivity" 
 android:orientation="vertical" 
 > 
 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="Please enter a number" /> 
 <EditText 
  android: 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  /> 
 <Button 
  android: 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="Call" 
  /> 
 
</LinearLayout> 

Code in mainactivity:

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class MainActivity extends Activity { 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  (savedInstanceState); 
  setContentView(.activity_main); 
   
  //Set the button to listen click  //1. Get the button object  Button bt = (Button) findViewById(.bt_call);//Button class is a subclass of View, and it is necessary to transform downward.  //2. Set listening  (new MyListener()); 
 } 
 
 class MyListener implements OnClickListener{ 
 
  //This method is called when the button is clicked  @Override 
  public void onClick(View v) { 
   //Get the number entered by the user   EditText et = (EditText) findViewById(.et_phone); 
   String phone = ().toString(); 
    
   //We need to tell the system, our actions: I want to call   //Create an intent object   Intent intent = new Intent(); 
   //Encapsulate the call action ACTION_CALL into the intent object   (Intent.ACTION_CALL); 
   //Set to whom   (("tel:" + phone));//This tel: It must be added, indicating that I want to call.  Otherwise, there will be no call function, because this "protocol" is set in the call list file.    
   //Tell the action of the system and start the system calling function.   startActivity(intent); 
  } 
   
 } 
  
} 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.