Android SMS 전송 샘플코드  └ SMS/Email



젠장.

교재든 인터넷자료든 다들 왜 퍼미션을 하나 빼먹고 소개를 해 놓는지 모르겟다.

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

위 퍼미션에 대해서는 언급하지 않아서, 한참을 고민했다 -_-

 

 

추가해야 할 퍼미션은 아래 세개항목이다. 사실 수신퍼미션은 필요없다.

 <uses-permission android:name="android.permission.RECEIVE_SMS" />
 <uses-permission android:name="android.permission.SEND_SMS" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />

 

 

그리고 코드중간에 //1 과 //2의 주석이 있는데. 둘중 하나의 메서드만 호출해도 전송은 가능하다.

실 단말기에서 테스트 완료.

 

================================= 코드 ====================================

package com.test;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.content.IntentFilter;
import android.location.Address;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMSActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button sendBtn = (Button) findViewById(R.id.sendSmsBtn);
        sendBtn.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    EditText addrTxt = (EditText) SMSActivity.this.findViewById(R.id.addrEditText);
    EditText msgTxt = (EditText) SMSActivity.this.findViewById(R.id.msgEditText);
    
    try {
     // 1
     //sendSmsMessage(addrTxt.getText().toString(), msgTxt.getText().toString());
     
     // 2
     sendSMS(addrTxt.getText().toString(), msgTxt.getText().toString());
     
     Toast.makeText(SMSActivity.this, "SMS 발송 완료", Toast.LENGTH_LONG).show();
     
    } catch (Exception e) {
     Toast.makeText(SMSActivity.this, e.toString(), Toast.LENGTH_LONG).show();
     e.printStackTrace();
    }
   }
        });
    }

 protected void sendSmsMessage(String address, String message) throws Exception {
  // TODO Auto-generated method stub
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(address, null, message, null, null);       

 }
 
 private void sendSMS(String phoneNumber, String message) {       
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";
 
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);
 
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);
 
        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));
 
        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered",
                                Toast.LENGTH_SHORT).show();
                        break;                       
                }
            }
        }, new IntentFilter(DELIVERED));       
 
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);       
    }
 
 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
 } 
}

 

=============================== 레이아웃 ===============================

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     >
  <TextView 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="수신번호"
   />
  <EditText
   android:id="@+id/addrEditText"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:phoneNumber="true"
   android:text="01012345678"
   />   
 </LinearLayout>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     >
  <TextView 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="문자메시지 : "
   />
  <EditText
   android:id="@+id/msgEditText"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="안녕 SMS"
   />
 </LinearLayout>
  
 <Button
  android:id="@+id/sendSmsBtn"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="문자 메세지 전송"
  />
  
</LinearLayout>

 


덧글

댓글 입력 영역
◀ 다음덧글    이전덧글 ▶


통계 위젯 (화이트)

1622
132
358626

red 210