Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Android Tutorials - สอนเขียน Android App ฟรี เขียนโปรแกรมแอนดรอยด์บน SmartPhone / Tablets > Android Send SMS ส่งข้อความ Message SMS เข้ามือถืออื่นง่าย ๆ ด้วย Android



Clound SSD Virtual Server

Android Send SMS ส่งข้อความ Message SMS เข้ามือถืออื่นง่าย ๆ ด้วย Android

Android Send SMS ในการส่ง SMS ข้อความ Message ไปยังหมายเลขโทนศัพท์อื่น ๆ บน Application ที่เราพัฒนาขึ้นบน Android นั้น สามารถทำได้ง่าย ๆ โดยใช้ Library ของ telephony.SmsManager และเขียน Code เพิ่มไปอีก 1-2 บรรทัดก็สามารถส่งได้แล้ว

Android Send SMS


smsManager.sendTextMessage("Phone No", null, "Message", null, null);

ในการส่ง SMS นั้นจะโยนหน้าที่สำหรับการส่งให้กับ OS ของ Android ที่ทำงานคู่กับเครื่อข่ายโทรศัพท์ ซึ่งเราสามารถเรียกส่ง SMS ได้เพียงสั้น ๆ เท่านี้

ก่อนการเขียนโปรแกรมสำหรับส่ง SMS จะต้องกำหนด Permission ให้กับไฟล์ AndroidManifest.xml ด้วยดังนี้
<uses-permission android:name="android.permission.SEND_SMS" />

แก้ไขไฟล์ AndroidManifest.xml

Android Send SMS



Example 1 การส่ง SMS แบบง่าย ๆ

โครงสร้างของไฟล์ประกอบด้วย 2 ไฟล์คือ MainActivity.java, activity_main.xml

Android Send SMS

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="To : "/>
 
    <EditText
        android:id="@+id/txtPhoneNo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="number" >
    </EditText>
 
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Message : " />
 
    <EditText
        android:id="@+id/txtMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:lines="3"
        android:gravity="top" />
 
    <Button
        android:id="@+id/btnSend"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send Message" />
 
</LinearLayout>









MainActivity.java
package com.myapp;

import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import android.app.Activity;


public class MainActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final Button btSend = (Button) findViewById(R.id.btnSend);
        final EditText sPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        final EditText sMessage = (EditText) findViewById(R.id.txtMessage);
        btSend.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				SmsManager smsManager = SmsManager.getDefault();
				String strPhoneNo = sPhoneNo.getText().toString();
				String strMessage = sMessage.getText().toString();
				smsManager.sendTextMessage(strPhoneNo, null, strMessage, null, null);
				Toast.makeText(getApplicationContext(), "SMS has been send.",
							Toast.LENGTH_LONG).show();
				
			}
        	 
		});
        
    }   
    
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}


Screenshot

Android Send SMS

เพิ่มเติม
สำหรับการทดสอบบน Emulator นั้นจะไม่สามารถส่งไปยังหมายเลขปลายทางได้จริง ๆ เพาะฉะนั้นถ้าจะทดสอบการสามารถส่ง SMS ได้หรือไม่นั้นให้ทดสอบด้วยการสร้าง Emulator ขึ้นมาอีก 1 ตัว ซึ่งหมายเลข Emulator ที่เป็นหมายเลขประจำเครื่องจะไม่เหมือนกัน สังเกตุได้จาก Title Bar ของ Emulator อย่างเช่นตัวนี้จะเป็นหมายเลข 5554 กรณีที่เปิดขึ้นมาใหม่อีกตัวก็จะได้ 5555 ซึ่งเราสามารถทดสอบส่งเข้าหมายเลขนี้ได้ และถ้าโปรแกรมทำงานผ่านเสร็จสมบูรณ์ ก็จะสามารถส่งข้อความไปยัง Emulator อีกตัวได้ในทันที





Example 2 การส่ง SMS โดยเรียก Application สำหรับส่ง SMS ที่อยู่บน Android

Android Send SMS

ทดสอบเปิด Application ชื่อว่า Messaging ที่อยู่บน Android OS

Android Send SMS

หน้าจอของโปรแกรม Messaging ที่อยู่บน Android OS

โครงสร้างของไฟล์ประกอบด้วย 2 ไฟล์คือ MainActivity.java, activity_main.xml

การเรียกใช้ Application ที่อยู่บน Android OS ผ่านการ Intent สามารถทำได้โดยใช้

	     Intent newIntent = new Intent(Intent.ACTION_VIEW);
	     newIntent.setType("vnd.android-dir/mms-sms");

จุดสำคัญจะอยู่ที่ vnd.android-dir/mms-sms ซึ่งเป็นตัวระบุ Type ว่าจะเรียก Application ตัวไหน

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
</LinearLayout>


MainActivity.java
package com.myapp;

import android.os.Bundle;
import android.view.Menu;
import android.app.Activity;
import android.content.Intent;


public class MainActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
	     Intent newIntent = new Intent(Intent.ACTION_VIEW);
	     newIntent.putExtra("address", "0819876107");  // Default Address to
	     newIntent.putExtra("sms_body", "My Messages"); // Default Message
	     newIntent.setType("vnd.android-dir/mms-sms");
	     startActivity(newIntent);
        
    }   
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

สามารถกำหนดหมายเลขปลายทาง และ ข้อความที่เป็นค่า Default ได้








Screenshot

Android Send SMS

ทดสอบส่ง SMS ผ่าน Intent ของ Messaging

สำหรับตัวอย่างที่ 2 นี้จะเป็นการเปิด Application ขึ้นมาทันทีหลังจากที่รันโปรแกรม แต่ในการใช้งานจริงจาก Application อาจจะใช้การทำเป้นปุ่ม Button และหลังจากที่คลิกที่ปุ่ม Button แล้วค่อยเปิด Application นี้

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-07-03 17:27:21 / 2017-03-26 20:12:49
  Download : No files
 Sponsored Links / Related

 
Android Call Phone (Intent.ACTION_CALL)
Rating :

 
Android Send Email (Intent.ACTION_SEND)
Rating :

 
Android and Notifications (NOTIFICATION_SERVICE)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่