* SmsReceiver.java : New > Other > Brodcast Receiver(AndroidManifest.xml에 Receiver로 등록됨)
public class SmsReceiver extends BroadcastReceiver {
private static final String TAG = "SmsReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceiver 호출됨");
Bundle bundle = intent.getExtras();
SmsMessage[] messages = parseSmsMessage(bundle);
if (messages != null && messages.length > 0) {
String sender = messages[0].getOriginatingAddress();
String contents = messages[0].getMessageBody();
Log.d(TAG, "sender : " + sender + ", contents : " + contents);
sendToActivity(context, sender, contents);
}
}
public void sendToActivity(Context context, String sender, String contents) {
Intent intent = new Intent(context, SmsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("sender", sender);
intent.putExtra("contents", contents);
context.startActivity(intent);
}
private SmsMessage[] parseSmsMessage(Bundle bundle) {
Object[] objs = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[objs.length];
for (int i = 0; i < objs.length; i++) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // SDK 23 이후
String format = bundle.getString("format");
messages[i] = SmsMessage.createFromPdu((byte[]) objs[i], format);
} else {
messages[i] = SmsMessage.createFromPdu((byte[]) objs[i]);
}
}
return messages;
}
}
* AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<receiver
android:name=".SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
* SmsActivity.java
public class SmsActivity extends AppCompatActivity {
TextView textView2;
TextView textView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
textView2 = findViewById(R.id.textView2);
textView3 = findViewById(R.id.textView3);
Intent intent = getIntent();
processIntent(intent);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
processIntent(intent);
}
public void processIntent(Intent intent) {
if (intent != null) {
String sender = intent.getStringExtra("sender");
String contents = intent.getStringExtra("contents");
textView2.setText(sender);
textView3.setText(contents);
}
}
}
* activity_sms.xml : SMS 수신화면
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SmsActivity">
<TextView
android:id="@+id/textView"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="수신한 문자 메시지" />
<TextView
android:id="@+id/textView2"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="발신번호" />
<TextView
android:id="@+id/textView3"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="문자내용" />
</LinearLayout>