Androidでは、

SmsManager

APIまたはデバイスの` Built-in SMS`アプリケーションを使用してSMSメッセージを送信できます。このチュートリアルでは、SMSメッセージを送信する2つの基本的な例を示します。

  1. SmsManager API

    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage( "phoneNo"、null、 "SMSメッセージ"、null、null);

  1. 内蔵のSMSアプリケーション

    インテントsendIntent =新しいインテント(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"/>

__P.SこのプロジェクトはEclipse 3.7で開発され、Samsung Galaxy S2(Android 2.3.3)でテストされました。

1. SmsManagerの例

Androidレイアウトファイルをテキストボックス(電話番号、SMSメッセージ)に、ボタンを押してSMSメッセージを送信します。


File:res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout__width="fill__parent"
    android:layout__height="fill__parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewPhoneNo"
        android:layout__width="wrap__content"
        android:layout__height="wrap__content"
        android:text="Enter Phone Number : "
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <EditText
        android:id="@+id/editTextPhoneNo"
        android:layout__width="fill__parent"
        android:layout__height="wrap__content"
        android:phoneNumber="true" >
    </EditText>

    <TextView
        android:id="@+id/textViewSMS"
        android:layout__width="wrap__content"
        android:layout__height="wrap__content"
        android:text="Enter SMS Message : "
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <EditText
        android:id="@+id/editTextSMS"
        android:layout__width="fill__parent"
        android:layout__height="wrap__content"
        android:inputType="textMultiLine"
        android:lines="5"
        android:gravity="top"/>

    <Button
        android:id="@+id/buttonSend"
        android:layout__width="fill__parent"
        android:layout__height="wrap__content"
        android:text="Send"/>

</LinearLayout>


File:SendSMSActivity.java

– `SmsManager`を介してSMSを送信するアクティビティ。

package com.mkyong.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

権限が必要です。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10"/>

    <uses-permission android:name="android.permission.SEND__SMS"/>

    <application
        android:debuggable="true"
        android:icon="@drawable/ic__launcher"
        android:label="@string/app__name" >
        <activity
            android:label="@string/app__name"
            android:name=".SendSMSActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

デモを見る:


smsmanager経由でSMSメッセージを送信する、title = "android-send-sms-message-example"、width = 317、高さ= 480

2.内蔵SMSアプリケーションの例

この例では、デバイスの組み込みSMSアプリケーションを使用してSMSメッセージを送信しています。


File:res/layout/main.xml

– ボタンのみ。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout__width="fill__parent"
    android:layout__height="fill__parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonSend"
        android:layout__width="fill__parent"
        android:layout__height="wrap__content"
        android:text="Send"/>

</LinearLayout>

ファイル:Send SMS Activity.java – ビルドインSMSを使用してSMSメッセージを送信するためのアクティビティクラス。

package com.mkyong.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();
                }
            }
        });
    }
}

デモを見る:


ビルドインSMSアプリケーション経由でSMSを送信する、title = "android-send-sms-message-example2"、width = 317、高さ= 480


build-smsアプリケーションでSMSを送信する、title = "android-send-sms-message-example2- 1 "、width = 317、height = 480

ソースコードをダウンロードする

それをダウンロードする – link://wp-content/uploads/2012/03/Android-Send-SMS-Example.zip[1。

Android-Send-SMS-Example.zip](16 KB)

ダウンロードする – link://wp-content/uploads/2012/03/Android-Build-In-SMS-Application-Example.zip[2。

Android-Build-In-SMS-Application-Example.zip](16 KB)