Phone Call trong Android



Android cung cấp các ứng dụng đã xây dựng sẵn cho Phone Call, trong một số tình huống chúng ta có thể cần tạo một Phone Call thông qua ứng dụng của chúng ta. Điều này có thể dễ dàng được thực hiện bằng việc sử dụng Implicit Intent với các Action thích hợp. Bạn cũng có thể sử dụng các lớp PhoneStateListener và TelephonyManager để giám sát các thay đổi trong một số trạng thái đàm thoại trên thiết bị.

Chương này liệt kê tất cả các bước đơn giản để tạo một ứng dụng mà có thể được sử dụng để tạo một Phone Call. Bạn có thể sử dụng Android Intent để tạo phone call bằng cách gọi tính năng Phone Call đã xây dựng sẵn của Android. Phần tiếp sẽ giải thích cho bạn các phần khác nhau của đối tượng Intent mà cần thiết để tạo một Call.

Đối tượng Intent: Action để tạo Phone Call

Bạn sẽ sử dụng ACTION_CALL để kích hoạt tính năng phone call có sẵn trong thiết bị Android. Sau đây là cú pháp cơ bản để tạo một Intent với ACTION_CALL.

Intent phoneIntent = new Intent(Intent.ACTION_CALL);

Bạn có thể sử dụng ACTION_DIAL thay cho ACTION_CALL. Trong trường hợp này bạn sẽ có tùy chọn để sửa đổi phone number trước khi tạo một call thay cho việc tạo một direct call.

Đối tượng Intent: Dữ liệu/Kiểu để tạo Phone Call

Để tạo một phone call tại một số 91-000-000-0000 đã cho, bạn cần xác định tel: dạng URI sử dụng phương thức setData() như sau:−

phoneIntent.setData(Uri.parse("tel:91-000-000-0000"));

Điểm thú vị là, để tạo một phone call, bạn không cần xác định bất cứ Extra Data hoặc Data Type nào.

Ví dụ

Sau đây là nội dung của Main Activity file đã được sửa đổi: src/MainActivity.java.

package com.example.saira_000.myapplication;import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;public class MainActivity extends Activity {
   Button b1;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      b1=(Button)findViewById(R.id.button);
      call();
   }
   
   private void call() {
      Intent in=new Intent(Intent.ACTION_CALL,Uri.parse("0000000000"));
      try{
         startActivity(in);
      }
      
      catch (android.content.ActivityNotFoundException ex){
         Toast.makeText(getApplicationContext(),"yourActivity is not founded",Toast.LENGTH_SHORT).show();
      }
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
   }
   
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      
      int id = item.getItemId();
      
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

Sau đây là nội dung của res/layout/activity_main.xml file −


   
   
      
   
      
   
      
   

Sau đây là nội dung của res/values/strings.xml để định nghĩa hai hằng −



   My Application
   Hello world!
   Settings

Sau đây là nội dung mặc định của AndroidManifest.xml



   
   
   
   
   
      
      
      
         
            
            
         
      
      
      
   

Cuối cùng, chạy ứng dụng Android vừa tạo ở trên.