📜  如何从您的 Android 应用程序拨打电话?

📅  最后修改于: 2022-05-13 01:55:51.799000             🧑  作者: Mango

如何从您的 Android 应用程序拨打电话?

在本文中,您将制作一个基本的 android 应用程序,该应用程序可用于通过您的 android 应用程序呼叫一些号码。

您可以在Intent的帮助下使用ACTION_CALL来执行此操作。基本上,Intent 是一个简单的消息对象,用于在 android 组件之间进行通信,例如活动、内容提供者、广播接收器和服务,这里用于拨打电话。此应用程序基本上包含一个活动,其中包含编辑文本以编写您想要拨打电话的电话号码和拨打该号码的按钮。

  • 步骤 1. Android-Manifest.xml 文件中的权限代码
    您需要获得用户的电话权限,并且在清单文件中添加了 CALL_PHONE 权限。这是清单文件的代码:
    Android-Manifest.xml
      
      
          
        
          
          
              
                  
                      
                      
                  
              
          
    


    activity_main.xml
    
    
    
       
          
       
          
    


    MainActivity.java
    package com.geeksforgeeks.phonecall;
      
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.content.Intent;
    import android.widget.EditText;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.net.Uri;
    import android.widget.Button;
      
    public class MainActivity extends AppCompatActivity {
      
        // define objects for edit text and button
        EditText edittext;
        Button button;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            // Getting instance of edittext and button
            button = findViewById(R.id.button);
            edittext = findViewById(R.id.editText);
      
            // Attach set on click listener to the button
            // for initiating intent
            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg)
                {
      
                    // getting phone number from edit text
                    // and changing it to String
                    String phone_number
                        = edittext.getText().toString();
      
                    // Getting instance of Intent
                    // with action as ACTION_CALL
                    Intent phone_intent
                        = new Intent(Intent.ACTION_CALL);
      
                    // Set data of Intent through Uri
                    // by parsing phone number
                    phone_intent
                        .setData(Uri.parse("tel:"
                                           + phone_number));
      
                    // start Intent
                    startActivity(phone_intent);
                }
            });
        }
    }


  • 步骤 2.activity_main.xml
    activity_main.xml 包含一个相对布局,其中包含编辑文本以编写您要拨打电话的电话号码和用于启动意图或拨打电话的按钮:

    activity_main.xml

    
    
    
       
          
       
          
    
    

  • 步骤 3. MainActivity。Java
    在主要活动中,创建 Intent 对象以将活动重定向到呼叫管理器,并将意图的操作属性设置为 ACTION_CALL。用户输入的电话号码通过 Uri 进行解析,并作为数据传递到 Intent 对象中,用于呼叫该电话号码. setOnClickListener附加到带有意图对象的按钮,以使用动作作为 ACTION_CALL 来拨打电话。这里是完整的代码:

    主要活动。Java

    package com.geeksforgeeks.phonecall;
      
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.content.Intent;
    import android.widget.EditText;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.net.Uri;
    import android.widget.Button;
      
    public class MainActivity extends AppCompatActivity {
      
        // define objects for edit text and button
        EditText edittext;
        Button button;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            // Getting instance of edittext and button
            button = findViewById(R.id.button);
            edittext = findViewById(R.id.editText);
      
            // Attach set on click listener to the button
            // for initiating intent
            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg)
                {
      
                    // getting phone number from edit text
                    // and changing it to String
                    String phone_number
                        = edittext.getText().toString();
      
                    // Getting instance of Intent
                    // with action as ACTION_CALL
                    Intent phone_intent
                        = new Intent(Intent.ACTION_CALL);
      
                    // Set data of Intent through Uri
                    // by parsing phone number
                    phone_intent
                        .setData(Uri.parse("tel:"
                                           + phone_number));
      
                    // start Intent
                    startActivity(phone_intent);
                }
            });
        }
    }
    

输出: