Api로 SMS 보내기



java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package ghj.com.sendsms;
 
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
import java.util.ArrayList;
 
public class MainActivity extends AppCompatActivity {
 
    SmsManager mSMSManager;
    EditText editBody;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mSMSManager = SmsManager.getDefault();
        editBody = (EditText)findViewById(R.id.editBody);
 
        Button btnSend = (Button)findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendSms();
            }
        });
    }
 
 
    public void sendSms(){
        //메시지
        String body = editBody.getText().toString();
        //160자 이하의 메시지 리스트로 만든다
        ArrayList<String> bodyList = mSMSManager.divideMessage(body);
 
        //송신 인텐트
        PendingIntent sentPI = PendingIntent.getBroadcast(this0new Intent("SMS_SENT"), 0);
        //수신 인텐트
        PendingIntent recvPI = PendingIntent.getBroadcast(this0new Intent("SMS_DELIVERED"), 0);
 
        registerReceiver(mSentReceiver, new IntentFilter("SMS_SENT"));
        registerReceiver(mRecvReceiver, new IntentFilter("SMS_DELIVERED"));
 
        //여러개의 SMS 메시지를 전송
        if(bodyList.size() > 1){
            ArrayList<PendingIntent> sentPIList = new ArrayList<>();
            ArrayList<PendingIntent> recvPIList = new ArrayList<>();
            for(int i=0; i<bodyList.size(); i++){
                sentPIList.add(sentPI);
                recvPIList.add(recvPI);
            }
            mSMSManager.sendMultipartTextMessage("010-1234-5678"null, bodyList, sentPIList, recvPIList);
        }
        //1개의 SMS 메시지를 전송
        else{
            mSMSManager.sendTextMessage("010-1234-5678"null, body, sentPI, recvPI);
        }
    }
 
    BroadcastReceiver mSentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
             switch (getResultCode()){
                case RESULT_OK:
                    Toast.makeText(MainActivity.this"SMS Send", Toast.LENGTH_SHORT).show();
                    break;
                 case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                     Toast.makeText(MainActivity.this"ERROR_GENERIC_FAILURE", Toast.LENGTH_SHORT).show();
                     break;
                 case SmsManager.RESULT_ERROR_NO_SERVICE:
                     Toast.makeText(MainActivity.this"ERROR_NO_SERVICE", Toast.LENGTH_SHORT).show();
                     break;
                 case SmsManager.RESULT_ERROR_NULL_PDU:
                     Toast.makeText(MainActivity.this"ERROR_NULL_PDU", Toast.LENGTH_SHORT).show();
                     break;
                 case SmsManager.RESULT_ERROR_RADIO_OFF:
                     Toast.makeText(MainActivity.this"ERROR_RADIO_OFF", Toast.LENGTH_SHORT).show();
                     break;
            }
        }
    };
 
    BroadcastReceiver mRecvReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode()){
                case RESULT_OK:
                    Toast.makeText(MainActivity.this"SMS Delivered", Toast.LENGTH_SHORT).show();
                    break;
                case RESULT_CANCELED:
                    Toast.makeText(MainActivity.this"SMS Delivered Fail", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
 
cs



xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ghj.com.sendsms.MainActivity">
 
    <EditText
        android:id="@+id/editBody"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
 
    <Button
        android:id="@+id/btnSend"
        android:text="SMS 보내기"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
 
cs



AndroidManifest.xml

1
2
3
4
5
6
7
    <application>
       ...
    </application>
 
 
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
cs



결과

+ Recent posts