안드로이드/Service
안드로이드 시스템 메시지
김어찐
2022. 6. 16. 13:44
728x90
안드로이드에서는 단말기에서 사건이 발생했을 경우 각 사건에 대해 정해진 메시지를 발생시킨다.
메시지가 발생되면 이에 반응하는 Broad Cast Receiver 들이 동작하게 된다.
실제로는 개발자가 각 사건에 대한 이름으로 Broad Cast Receiver를 등록해 놓으면 OS가 이를 찾아 동작시키는 방식이다.
안드로이드 8.0 부터 사용할 수 있는 시스템 메시지의 수가 줄어들었다.
https://developer.android.com/guide/components/broadcast-exceptions.html
암시적 브로드캐스트 예외 | Android 개발자 | Android Developers
백그라운드 제한에서 제외되는 암시적 브로드캐스트입니다.
developer.android.com
메인
package com.example.systemmessage
import android.Manifest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
val permissionList = arrayOf(
Manifest.permission.RECEIVE_BOOT_COMPLETED,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.RECEIVE_SMS,
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
requestPermissions(permissionList,0)
}
}
리시버
package com.example.systemmessage
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.telephony.SmsMessage
import android.widget.Toast
class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// This method is called when the BroadcastReceiver is receiving an Intent broadcast.
// BR의 이름으로 분기
when (intent.action) {
"android.intent.action.BOOT_COMPLETED" -> {
val t1 = Toast.makeText(context,"부팅완료",Toast.LENGTH_SHORT)
t1.show()
}
"android.provider.Telephony.SMS_RECEIVED" -> {
if (intent.extras != null) {
// 문자 메시지 정보 객체를 추출한다.
val pduObject = intent.extras?.get("pdus") as Array<Any?>
if (pduObject != null) {
for (obj in pduObject) {
// 문자 메세지 양식 객체를 추출한다.
val format = intent.extras?.getString("format")
// 문자 메세지 객체를 생성한다.
val currentSMS = SmsMessage.createFromPdu(obj as ByteArray?,format)
val showMessage = "전화번호 ${currentSMS.displayOriginatingAddress}\n 내뇽 : ${currentSMS.displayMessageBody}"
val t1 = Toast.makeText(context,showMessage,Toast.LENGTH_SHORT)
t1.show()
}
}
}
}
}
}
}
메니패스트
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.systemmessage">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SystemMessage"
tools:targetApi="31">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
</manifest>
728x90