Send SMS trong Android P2

Đây là phần tiếp theo hướng dẫn các bạn học lập trình android trong việc send SMS trong android
Sử dụng Intent đã xây dựng sẵn để gửi SMS
Đối tượng Intent: Action để gửi SMS

Bạn có thể sử dụng Android Intent để gửi SMS bằng việc gọi tính năng SMS đã xây dựng sẵn của Android. Khu vực dưới giải thích các phần khác nhau trong đối tượng Intent mà cần thiết để gửi một SMS.
Bạn sẽ sử dụng ATION_VIEW để chạy một SMS Client đã được cài đặt trên thiết bị Android của bạn. Sau đây là cú pháp cơ bản để tạo một Intent với ACTION_VIEW.
Intent smsIntent = new Intent(Intent.ACTION_VIEW);

Đối tượng Intent: Dữ liệu/Kiểu để gửi SMS

Để gửi một SMS, bạn cần xác định smsto: dạng URI sử dụng phương thức setData() và kiểu dữ liệu sẽ là vnd.android-dir/mms-sms sử dụng phương thức setType(), như sau:
smsIntent.setData(Uri.parse("smsto:")); 
smsIntent.setType("vnd.android-dir/mms-sms");

Đối tượng Intent: Extra để gửi SMS

Android hỗ trợ sẵn để thêm số điện thoại và thông điệp text để gửi một SMS, như sau:smsIntent.putExtra("address" , new String("0123456789;3393993300")); smsIntent.putExtra("sms_body" , "Test SMS to Angilla");
Ở đây, address và sms_body là phân biệt kiểu chữ và nên được xác định chỉ trong dạng các ký tự nhỏ. Bạn có thể xác định nhiều hơn một số trong chuỗi đơn nhưng được phân biệt nhau bởi dấu chấm phảy.

Ví dụ

Ví dụ sau minh họa cho bạn cách sử dụng đối tượng Intent để chạy SMS Client để gửi một SMS tới các người nhận đã cho.
Sau đây là nội dung của Main Activity file đã được sửa đổi:src/com.example.tutorialspoint/MainActivity.java.package com.example.tutorialspoint;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     Button startBtn = (Button) findViewById(R.id.button);
     startBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
           sendSMS();
        }
     });
  }

  protected void sendSMS() {
     Log.i("Send SMS", "");
     Intent smsIntent = new Intent(Intent.ACTION_VIEW);
   
     smsIntent.setData(Uri.parse("smsto:"));
     smsIntent.setType("vnd.android-dir/mms-sms");
     smsIntent.putExtra("address"  , new String ("01234"));
     smsIntent.putExtra("sms_body"  , "Test ");
   
     try {
        startActivity(smsIntent);
        finish();
        Log.i("Finished sending SMS...", "");
     }
     catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MainActivity.this,
        "SMS faild, please try again later.", 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.main, menu);
     return true;
  }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context=".MainActivity">
 
  <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Drag and Drop Example"
     android:id="@+id/textView"
     android:layout_alignParentTop="true"
     android:layout_centerHorizontal="true"
     android:textSize="30dp" />
   
  <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Tutorials Point "
     android:id="@+id/textView2"
     android:layout_below="@+id/textView"
     android:layout_centerHorizontal="true"
     android:textSize="30dp"
     android:textColor="#ff14be3c" />
   
  <ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/imageView"
     android:src="@drawable/abc"
     android:layout_marginTop="48dp"
     android:layout_below="@+id/textView2"
     android:layout_centerHorizontal="true" />
   
  <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Compose SMS"
     android:id="@+id/button"
     android:layout_below="@+id/imageView"
     android:layout_alignRight="@+id/textView2"
     android:layout_alignEnd="@+id/textView2"
     android:layout_marginTop="54dp"
     android:layout_alignLeft="@+id/imageView"
     android:layout_alignStart="@+id/imageView" />
   
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">tutorialspoint</string>
  <string name="action_settings">Settings</string>
</resources>

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
Sau đây là nội dung mặc định của AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.tutorialspoint"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
     android:minSdkVersion="8"
     android:targetSdkVersion="22" />
   
  <application
     android:allowBackup="true"
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:theme="@style/AppTheme" >
   
     <activity
        android:name="com.example.tutorialspoint.MainActivity"
        android:label="@string/app_name" >
     
        <intent-filter>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
     
     </activity>
   
  </application>
</manifest>

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



Chọn thiết bị mobile và sau đó kiểm tra thiết bị mobile của bạn sẽ hiển thị màn hình sau:




Chọn thiết bị mobile và sau đó kiểm tra thiết bị mobile của bạn sẽ hiển thị màn hình sau:




Bạn có thể sửa đổi các trường mặc định đã cho và sau đó sử dụng nút Send SMS để gửi SMS của bạn tới người nhận.

0 nhận xét: