반응형
우리는 앱을 쓰면서 많은 곳에서 "문자 메시지 서비스"를 받고 있습니다.
그러한 문자 서비스를 내가 만든 앱에도 넣어보고 싶었습니다...
private void sendSMS(String phoneNumber, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
String strMessage = "인증번호 : " + message;
// 각각 위에서부터 문자 전송, 문자 수신에 관련하여 sendTextMessage()에 넘겨줄 값들입니다
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(getApplicationContext(), "SMS 전송", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getApplicationContext(), "Generic failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getApplicationContext(), "No service", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getApplicationContext(), "Null PDU", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getApplicationContext(), "Radio off", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getApplicationContext(), "SMS 전송 완료", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getApplicationContext(), "SMS 전송 실패", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, strMessage, senTPI, deliveredPI);
}
이런식으로 구현한 뒤, sms를 보내고 싶은 곳에서 보내고 싶은 사람의 번호와 보낼 메시지를 적어주면 끝~
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
허가해주는거 잊지 마세요~!!
반응형
'Android > Java' 카테고리의 다른 글
간단한 SplashActivity 만들기 (0) | 2018.05.09 |
---|---|
ProgressbarUtil 만들기 (0) | 2018.05.08 |
마켓에 올린 앱의 "버전" 가져오는 방법 (0) | 2018.05.06 |
간단한 view pager 사용법 (0) | 2018.05.03 |
권한요청하기 (0) | 2018.05.02 |