I create Custom Intent
as below.
Intent().also { intent ->
intent.setAction("STEP_COUNT_NOTIFICATION")
intent.putExtra("stepCount", todayTotalStepCount)
sendBroadcast(intent)
}
And I add this Custom Intent in my Broadcast Receiver tag of AndroidManifest
.
<receiver
android:name=".BroadcastReceiver.BroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_DATE_CHANGED" />
<action android:name="com.chungchunon.chunchunon_android.STEP_COUNTER_NOTIFICATION" />
</intent-filter>
</receiver>
Since I use two Intent, one is official intent and the other is custom, I need to distinguish received Intent.
onReceive
function of BroadcastReceiver
override fun onReceive(context: Context?, intent: Intent?) {
val intentAction = intent!!.action
when (intentAction) {
Intent.ACTION_DATE_CHANGED -> {
// todayTotalStepCount = 0
var todayStepCountSet = hashMapOf<String, Int?>(
"todayStepCount" to 0
)
userDB
.document("$userId")
.set(todayStepCountSet, SetOptions.merge())
}
???? ->
}
}
How can I get STEP_COUNT_NOTIFICATION which is my custom intent?
CodePudding user response:
Your Intent
action needs to match the android:name
for the corresponding <intent-filter>
. Yours do not.
You have:
intent.setAction("STEP_COUNT_NOTIFICATION")
This has an action string of STEP_COUNT_NOTIFICATION
.
You also have:
<action android:name="com.chungchunon.chunchunon_android.STEP_COUNTER_NOTIFICATION" />
This has an action string of com.chungchunon.chunchunon_android.STEP_COUNTER_NOTIFICATION
.
Those two are not the same. They need to be the same. I recommend using com.chungchunon.chunchunon_android.STEP_COUNTER_NOTIFICATION
in both places.
CodePudding user response:
First, create a public
constant for your custom action
:
companion object {
const val ACTION_STEP_COUNTER_NOTIFICATION = "com.chungchunon.chunchunon_android.STEP_COUNTER_NOTIFICATION"
}
For your Intent
, use the same action
:
Intent().also { intent ->
intent.setAction(ACTION_STEP_COUNTER_NOTIFICATION)
intent.putExtra("stepCount", todayTotalStepCount)
sendBroadcast(intent)
}
Also, use the same action
in your BroadcastReceiver
:
override fun onReceive(context: Context?, intent: Intent?) {
val intentAction = intent!!.action
when (intentAction) {
Intent.ACTION_DATE_CHANGED -> {
// todayTotalStepCount = 0
var todayStepCountSet = hashMapOf<String, Int?>(
"todayStepCount" to 0
)
userDB
.document("$userId")
.set(todayStepCountSet, SetOptions.merge())
}
// Your custom action
ACTION_STEP_COUNTER_NOTIFICATION -> { // Add your code here }
}
}
Lastly, for AndroidManifest.xml
:
<receiver
android:name=".BroadcastReceiver.BroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_DATE_CHANGED" />
// action should be the same as the value of ACTION_STEP_COUNTER_NOTIFICATION
<action android:name="com.chungchunon.chunchunon_android.STEP_COUNTER_NOTIFICATION" />
</intent-filter>
</receiver>
action
name should be the same for Intent
, AndroidManifest.xml
and BroadcastReceiver