Android-sending-sms
Android-SMSの送信
Androidでは、SmsManager APIまたはデバイスの組み込み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許可*が必要です。
<uses-permission android:name="android.permission.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 の内容を示します。
package com.example.finddevguides;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ;
Button sendBtn;
EditText txtphoneNo;
EditText txtMessage;
String phoneNo;
String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editText);
txtMessage = (EditText) findViewById(R.id.editText2);
sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
}
protected void sendSMSMessage() {
phoneNo = txtphoneNo.getText().toString();
message = txtMessage.getText().toString();
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
return;
}
}
}
}
}
以下は res/layout/activity_main.xml ファイルの内容です-
'_ここにabcはfinddevguidesロゴについて示しています_
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sending SMS Example"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_below="@+id/textView1"
android:layout_alignRight="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Enter Phone Number"
android:phoneNumber="true"
android:textColorHint="@color/abc_primary_text_material_dark"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:textColorHint="@color/abc_primary_text_material_dark"
android:layout_alignRight="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton"
android:hint="Enter SMS"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Sms"
android:id="@+id/btnSendSMS"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"/>
</RelativeLayout>
以下は、2つの新しい定数を定義する res/values/strings.xml の内容です-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">finddevguides</string>
</resources>
以下は、 AndroidManifest.xml のデフォルトのコンテンツです-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finddevguides" >
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.finddevguides.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>
*finddevguides* アプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Android Studioインストーラーは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。
これで、目的の携帯電話番号とその番号で送信されるテキストメッセージを入力できます。 最後に[SMSを送信]ボタンをクリックして、SMSを送信します。 SMSを受信者に配信するために、GSM/CDMA接続が正常に機能していることを確認してください。
カンマで区切られた多数のSMSを取得し、プログラム内でそれらを配列文字列に解析する必要があります。最後に、ループを使用して、指定されたすべての番号にメッセージを送信できます。 これが、独自のSMSクライアントを作成する方法です。 次のセクションでは、既存のSMSクライアントを使用してSMSを送信する方法を示します。
組み込みインテントを使用してSMSを送信する
Androidの組み込みSMS機能を呼び出すことにより、Android Intentを使用してSMSを送信できます。 次のセクションでは、SMSの送信に必要なIntentオブジェクトのさまざまな部分について説明します。
インテントオブジェクト-SMSを送信するアクション
*ACTION_VIEW* アクションを使用して、AndroidデバイスにインストールされているSMSクライアントを起動します。 以下は、ACTION_VIEWアクションでインテントを作成する簡単な構文です。
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
インテントオブジェクト-SMSを送信するデータ/タイプ
SMSを送信するには、setData()メソッドを使用してURIとして* smsto:を指定する必要があり、データ型は次のようにsetType()メソッドを使用して *vnd.android-dir/mms-sms になります-
smsIntent.setData(Uri.parse("smsto:"));
smsIntent.setType("vnd.android-dir/mms-sms");
インテントオブジェクト-SMSを送信するための追加
Androidには、次のようにSMSを送信するための電話番号とテキストメッセージを追加する組み込みサポートがあります-
smsIntent.putExtra("address" , new String("0123456789;3393993300"));
smsIntent.putExtra("sms_body" , "Test SMS to Angilla");
'_ここで、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 の内容を示します。
package com.example.finddevguides;
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;
}
}
以下は res/layout/activity_main.xml ファイルの内容です-
'_ここにabcはfinddevguidesロゴについて示しています_
<?xml version="1.0" encoding="utf-8"?>
<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>
以下は、2つの新しい定数を定義する res/values/strings.xml の内容です-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">finddevguides</string>
</resources>
以下は、 AndroidManifest.xml のデフォルトのコンテンツです-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finddevguides" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.finddevguides.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>
*finddevguides* アプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。
オプションとしてモバイルデバイスを選択し、次の画面を表示するモバイルデバイスを確認します-
今すぐ Compose SMS ボタンを使用して、以下に示すAndroid組み込みSMSクライアントを起動します-
指定されたデフォルトフィールドのいずれかを変更し、最後にSMS送信ボタンを使用して、指定された受信者にSMSを送信できます。