Android-sending-sms
Android-SMSの送信
Androidでは、SmsManager APIまたはデバイスの組み込みSMSアプリケーションを使用してSMSを送信できます。 このチュートリアルでは、SMSメッセージを送信する2つの基本的な例を示します-
ビルトインSMSアプリケーション
もちろん、両方とも* SEND_SMS許可*が必要です。
上記のメソッドとは別に、SmsManagerクラスで使用できる他の重要な関数はほとんどありません。 これらの方法は以下のとおりです-
Sr.No. | Method & Description |
---|---|
1 |
ArrayList<String> divideMessage(String text) このメソッドは、メッセージテキストをいくつかのフラグメントに分割します。フラグメントは、SMSメッセージの最大サイズ以下です。 |
2 |
static SmsManager getDefault() このメソッドは、SmsManagerのデフォルトインスタンスを取得するために使用されます |
3 |
void sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent) このメソッドは、特定のアプリケーションポートにデータベースのSMSを送信するために使用されます。 |
4 |
void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents) マルチパートテキストベースのSMSを送信します。 |
5 |
void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent) テキストベースのSMSを送信します。 |
例
次の例は、SmsManagerオブジェクトを使用して特定の携帯電話番号にSMSを送信する方法を実際に示しています。
'_この例を試すには、最新のAndroid OSを搭載した実際のモバイルデバイスが必要です。そうでない場合は、エミュレータが動作しないことがあります。_
Step | Description |
---|---|
1 | You will use Android Studio IDE to create an Android application and name it as finddevguides under a package com.example.finddevguides. |
2 | Modify src/MainActivity.java file and add required code to take care of sending sms. |
3 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. I’m adding a simple GUI to take mobile number and SMS text to be sent and a simple button to send SMS. |
4 | No need to define default string constants at res/values/strings.xml. Android studio takes care of default constants. |
5 | Modify AndroidManifest.xml as shown below |
6 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
次に、変更されたメインアクティビティファイル src/com.example.finddevguides/MainActivity.java の内容を示します。
以下は res/layout/activity_main.xml ファイルの内容です-
'_ここにabcはfinddevguidesロゴについて示しています_
以下は、2つの新しい定数を定義する res/values/strings.xml の内容です-
以下は、 AndroidManifest.xml のデフォルトのコンテンツです-
これで、目的の携帯電話番号とその番号で送信されるテキストメッセージを入力できます。 最後に[SMSを送信]ボタンをクリックして、SMSを送信します。 SMSを受信者に配信するために、GSM/CDMA接続が正常に機能していることを確認してください。
カンマで区切られた多数のSMSを取得し、プログラム内でそれらを配列文字列に解析する必要があります。最後に、ループを使用して、指定されたすべての番号にメッセージを送信できます。 これが、独自のSMSクライアントを作成する方法です。 次のセクションでは、既存のSMSクライアントを使用してSMSを送信する方法を示します。
組み込みインテントを使用してSMSを送信する
Androidの組み込みSMS機能を呼び出すことにより、Android Intentを使用してSMSを送信できます。 次のセクションでは、SMSの送信に必要なIntentオブジェクトのさまざまな部分について説明します。
インテントオブジェクト-SMSを送信するアクション
インテントオブジェクト-SMSを送信するデータ/タイプ
SMSを送信するには、setData()メソッドを使用してURIとして* smsto:を指定する必要があり、データ型は次のようにsetType()メソッドを使用して *vnd.android-dir/mms-sms になります-
インテントオブジェクト-SMSを送信するための追加
Androidには、次のようにSMSを送信するための電話番号とテキストメッセージを追加する組み込みサポートがあります-
'_ここで、addressとsms_bodyは大文字と小文字が区別されるため、小さい文字のみで指定する必要があります。 1つの文字列に複数の数値を指定できますが、セミコロン(;)で区切ります。_
例
次の例は、Intentオブジェクトを使用してSMSクライアントを起動し、指定された受信者にSMSを送信する方法を実際に示しています。
'_この例を試すには、最新のAndroid OSを搭載した実際のモバイルデバイスが必要です。そうでない場合は、エミュレータが動作しないことがあります。_
Step | Description |
---|---|
1 | You will use Android studio IDE to create an Android application and name it as finddevguides under a package com.example.finddevguides. |
2 | Modify src/MainActivity.java file and add required code to take care of sending SMS. |
3 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. I’m adding a simple button to launch SMS Client. |
4 | No need to define default constants.Android studio takes care of default constants. |
5 | Modify AndroidManifest.xml as shown below |
6 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
次に、変更されたメインアクティビティファイル src/com.example.finddevguides/MainActivity.java の内容を示します。
以下は res/layout/activity_main.xml ファイルの内容です-
'_ここにabcはfinddevguidesロゴについて示しています_
以下は、2つの新しい定数を定義する res/values/strings.xml の内容です-
以下は、 AndroidManifest.xml のデフォルトのコンテンツです-
オプションとしてモバイルデバイスを選択し、次の画面を表示するモバイルデバイスを確認します-
今すぐ Compose SMS ボタンを使用して、以下に示すAndroid組み込みSMSクライアントを起動します-
指定されたデフォルトフィールドのいずれかを変更し、最後にSMS送信ボタンを使用して、指定された受信者にSMSを送信できます。