AndroidでSMSメッセージを送信する方法
Androidでは、SmsManager
APIまたはデバイスのBuilt-in SMS
アプリケーションを使用してSMSメッセージを送信できます。 このチュートリアルでは、SMSメッセージを送信する2つの基本的な例を示します。
-
SmsManager API
SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);
-
組み込みSMSアプリケーション
Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.putExtra("sms_body", "default content"); sendIntent.setType("vnd.android-dir/mms-sms"); startActivity(sendIntent);
もちろん、どちらもSEND_SMSの許可が必要です。
P.S This project is developed in Eclipse 3.7, and tested with Samsung Galaxy S2 (Android 2.3.3).
Note
組み込みのSMSアプリケーションソリューションは、デバイスにすべてを処理させるため、最も簡単な方法です。
1. SmsManagerの例
Androidレイアウトファイルをテキストボックス(電話番号、SMSメッセージ)に送信し、SMSメッセージを送信するボタン。
ファイル:res / layout / main.xml
File : SendSMSActivity.java –SmsManager
を介してSMSを送信するアクティビティ。
package com.example.android; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class SendSMSActivity extends Activity { Button buttonSend; EditText textPhoneNo; EditText textSMS; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonSend = (Button) findViewById(R.id.buttonSend); textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo); textSMS = (EditText) findViewById(R.id.editTextSMS); buttonSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String phoneNo = textPhoneNo.getText().toString(); String sms = textSMS.getText().toString(); try { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phoneNo, null, sms, null, null); Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show(); e.printStackTrace(); } } }); } }
File : AndroidManifest.xml、SEND_SMS権限が必要です。
デモを見る:
2. 組み込みSMSアプリケーションの例
この例では、デバイスの組み込みSMSアプリケーションを使用してSMSメッセージを送信しています。
File : res/layout/main.xml –ボタンのみ。
File : SendSMSActivity.java –組み込みのSMSインテントを使用してSMSメッセージを送信するアクティビティクラス。
package com.example.android; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class SendSMSActivity extends Activity { Button buttonSend; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonSend = (Button) findViewById(R.id.buttonSend); buttonSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.putExtra("sms_body", "default content"); sendIntent.setType("vnd.android-dir/mms-sms"); startActivity(sendIntent); } catch (Exception e) { Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show(); e.printStackTrace(); } } }); } }
デモを見る:
ソースコードをダウンロード
ダウンロード–1. Android-Send-SMS-Example.zip(16 KB)
ダウンロード–2. Android-Build-In-SMS-Application-Example.zip(16 KB)