Android

안드로이드 SMS 문자메세지 보내기

Machine_웅 2018. 10. 25. 13:44
728x90
반응형
  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.js.sendsms_example">

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

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

퍼미션으로  SMS 를 넣어준다.

 

 

 

 

 

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.js.sendsms_example.MainActivity">

    <TextView
    android:id="@+id/textViewPhoneNo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="44dp"
    android:text="수신 전화번호"
    android:textColor="@color/colorDefault"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="12dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    <EditText
    android:id="@+id/editTextPhoneNo"
    android:layout_width="344dp"
    android:layout_height="41dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:inputType="phone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textViewPhoneNo"></EditText>

    <TextView
    android:id="@+id/textViewSMS"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="메세지를 입력하세요."
    android:textColor="@color/colorDefault"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="12dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editTextPhoneNo" />

    <EditText
    android:id="@+id/editTextSMS"
    android:layout_width="351dp"
    android:layout_height="106dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:gravity="top"
    android:inputType="textMultiLine"
    android:lines="5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.529"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textViewSMS" />

    <Button
    android:id="@+id/buttonSend"
    android:layout_width="166dp"
    android:layout_height="56dp"
    android:layout_marginBottom="160dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="보내기"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editTextSMS"
    app:layout_constraintVertical_bias="1.0" />

</android.support.constraint.ConstraintLayout>

 

 

레이아웃 작성

 

 

 

 

 


            package com.js.sendsms_example;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

    public class MainActivity extends AppCompatActivity {

        Button buttonSend;
        EditText textPhoneNo;
        EditText textSMS;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            buttonSend = (Button) findViewById(R.id.buttonSend);
            textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
            textSMS = (EditText) findViewById(R.id.editTextSMS);

            //버튼 클릭이벤트
            buttonSend.setOnClickListener(new View.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(), "전송 완료!", Toast.LENGTH_LONG).show();
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }

                }
            });

        }
    }



전송 관련 코드 

 

 

 

출처 : https://m.blog.naver.com/PostView.nhn?blogId=cosmosjs&logNo=221149013995&proxyReferer=https%3A%2F%2Fwww.google.com%2F

 

안드로이드 스튜디오 : 문자(SMS) 보내기

안드로이드 문자 전송하는 간단한 앱을 만들어 보자.SMS 퍼미션을 주자.화면 레이아웃을 간단히 만들자....

blog.naver.com

 

 

https://link2me.tistory.com/1207

 

Android 문자 보내는 방법2(Using the SmsManager API)

android.telephone.SmsManager class를 이용하여, API 코드 2줄로 SMS를 자동으로 보낼 수 있다. SMS 메시지 보내는 코드SmsManager smsManager = SmsManager.getDefault(); // Get the default instance of SmsManagersmsManager.sendTextMessage

link2me.tistory.com

 

728x90
반응형

'Android' 카테고리의 다른 글

( 스크랩 ) 안드로이드 핸들러  (0) 2019.01.08
안드로이드 php Json  (1) 2018.11.14
안드로이드 MediaController  (0) 2018.08.04
리사이클러뷰 아이템 클릭 이벤트  (0) 2018.07.26
프래그먼트 생명주기  (0) 2018.07.23