Created
October 5, 2022 07:02
-
-
Save devrath/6994b19c58c856a2aa9b782fd4db4514 to your computer and use it in GitHub Desktop.
Receiver for app to app communication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.droid.code.demos.application_receiver | |
import android.content.Intent | |
import android.content.IntentFilter | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import com.droid.code.databinding.ActivityDynamicRecieverBinding | |
import com.droid.code.demos.application_receiver.reciever.ApplicationReceiverBroadcastReceiver | |
import com.droid.code.demos.dynamic_receiver.reciever.CustomDynamicBroadcastReceiver | |
class ApplicationReceiverActivity : AppCompatActivity() { | |
private lateinit var binding: ActivityDynamicRecieverBinding | |
lateinit var receiver : ApplicationReceiverBroadcastReceiver | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityDynamicRecieverBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
registerReceiver() | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
unRegisterReceiver() | |
} | |
private fun registerReceiver() { | |
receiver = ApplicationReceiverBroadcastReceiver() | |
IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED).also { registerReceiver(receiver,it) } | |
} | |
private fun unRegisterReceiver() { | |
unregisterReceiver(receiver) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.droid.code.demos.application_receiver.reciever | |
import android.content.BroadcastReceiver | |
import android.content.Context | |
import android.content.Intent | |
import android.widget.Toast | |
const val KEY_ACTION = "com.droid.code.ACTION_SEND" | |
const val KEY_DATA = "com.droid.code.EXTRA_DATA" | |
class ApplicationReceiverBroadcastReceiver : BroadcastReceiver() { | |
override fun onReceive(context: Context?, intent: Intent) { | |
when(intent?.action){ | |
KEY_ACTION ->{ | |
val string = intent.getStringExtra(KEY_DATA) | |
Toast.makeText(context,"Data recieved is ${string}", Toast.LENGTH_LONG).show() | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---> Sender code to be place in another app - <SENDER> | |
val intent = Intent("com.droid.code.ACTION_SEND") | |
intent.putExtra("com.droid.code.EXTRA_DATA","sender send data") | |
sendBroadcast(intent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment