橘子味的心
标题:5.2 Android拨打电话

借助于 Intent 可以轻松实现拨打电话的应用程序。只需声明一个拨号的 Intent 对象,并使用 startActivity() 方法启动即可。

创建 Intent 对象的代码为 Intent intent = new Intent(action,uri),其中 URI 是要拨叫的号码数据,通过 Uri.parse() 方法把“tel:1234”格式的字符串转换为 URI。

而 Action 有两种使用方式:
  • Intent.Action_CALL。是直接进行呼叫的方式,这种方式需要应用程序具有 android.permission.CALL_PHONE 权限。
  • Intent.Action_DIAL。不是不直接进行呼叫,而是启动 Android 系统的拨号应用程序,然后由用户进行拨号。这种方式不需要任何权限的设置。

实例 phoneDemo 演示了使用 Intent.Action_CALL 方式进行拨号的过程,运行效果如图 1 所示。

使用Intent.Action_CALL方式拨号
图 1  使用Intent.Action_CALL方式拨号

实例 phoneDemo 中 main.xml 的代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6.  
  7. <EditText
  8. android:id="@+id/edittext"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginLeft="40dp"
  12. android:layout_marginTop="30dp" />
  13.  
  14. <Button
  15. android:id="@+id/button"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_marginLeft="80dp"
  19. android:layout_marginTop="40dp"
  20. android:text="拨打电话" />
  21.  
  22. </LinearLayout>
实例 phoneDemo 中 AndroidManifest.xml 的代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="introduction.android.phonedemo">
  4.  
  5. <uses-permission android:name="android.permission.CALL_PHONE" />
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity">
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" />
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. </application>
  20. </manifest>
实例 phoneDemo 中 PhoneDemoActivity.java 的具体实现代码如下:
  1. package introduction.android.phonedemo;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.view.View;
  8.  
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12.  
  13. public class MainActivity extends Activity {
  14.  
  15. /**
  16. * Called when the activity is first created.
  17. */
  18. private Button button;
  19. private EditText edittext;
  20.  
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. button = (Button) findViewById(R.id.button);
  26. button.setOnClickListener(new buttonListener());
  27. }
  28.  
  29. class buttonListener implements OnClickListener {
  30. @Override
  31. public void onClick(View v) {
  32. // TODO Auto-generated method stub
  33. edittext = (EditText) findViewById(R.id.edittext);
  34. String number = edittext.getText().toString();
  35. Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
  36. //startActivity(intent);
  37. }
  38.  
  39. }
  40.  
  41. }
其中:

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
startActivity(intent);

通过 Intent.ACTION_CALL 建立了一个进行拨号的 Intent 请求,并使用 startActivity 直接启动 Android 系统的拨号程序进行呼叫。

若在实例 PhoneDemo 中,将 PhoneDemoActivity.java 中的代码:

Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number));

修改为:

Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+number));

最后,单击“拨打电话”按钮后不再直接呼叫,而是只运行 Android 系统默认的拨号程序,用户还拥有进一步决定下一步操作的权限,运行效果如图 2 所示。
拨打电话
图 2  拨打电话