안드로이드에서 브로드케스트 중 SMS를 수신받아서 발신자, 내용, 날짜 등의 정보를 얻어오기 위한 셈플

 

 

 

# SMSReceiver.java

package com.example.wookoa.wookoa_10;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

import java.util.Date;

public class SMSReceiver extends BroadcastReceiver {
private static final String TAG = "SMSReceiver"; // Java Class 명과 동일.

@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.d(TAG, "onReceive Function Call");

Bundle bundle = intent.getExtras();
SmsMessage[] smsMsg = parseSmsMsg(bundle);

if(smsMsg.length > 0){
String sender = smsMsg[0].getOriginatingAddress(); // 발신자
Log.d(TAG, "Sender: " + sender);

String contents = smsMsg[0].getMessageBody().toString(); // SMS 내용
Log.d(TAG, "Contents: " + contents);

Date date = new Date(smsMsg[0].getTimestampMillis()); // 수신 날짜 및 시각
Log.d(TAG, "Date: " + date);
}
}

private SmsMessage[] parseSmsMsg(Bundle bundle){
Object[] objs = (Object[]) bundle.get("pdus"); // 'pdus' 키워드를 파라미터로 넣어주면, SMS 관련된 데이터를 가지고 옴.
SmsMessage[] tempMsg = new SmsMessage[objs.length];

for(int i=0; i < objs.length; i++){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ // Build.VERSION_CODES.M 에서 'M'은 마시멜로우 버전 상수
String format = bundle.getString("format");
tempMsg[i] = SmsMessage.createFromPdu((byte[]) objs[i], format);
}
else {
tempMsg[i] = SmsMessage.createFromPdu((byte[]) objs[i]);
}
}

return tempMsg;
}
}

 

 

# AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wookoa.wookoa_10">

<uses-permission android:name="android.permission.RECEIVE_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>

<receiver
android:name=".SMSReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>

 

 

# build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.wookoa.wookoa_10"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}

 

 

 

Private comment