Create Explicit Broadcasts and send Broadcasts inside App and to other Apps. Broadcastreceivers in android.

useful video : https://youtu.be/mMDsew46gFw

Sending explicit Broadcasts is some what same as sending explicit intent

We can send/receive  broadcasts inside the app and From other apps
----------------------------------------------------------------------------------------
USING EXPLICIT BROADCAST INSIDE THE APP :


Step 1]  Create a Broadcast receiver class .


Broadcastreceiverrrclass.java
package com.deepesh.broadcastapp3;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class Broadcatsreceiverrr extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

Toast.makeText(context, "Broadcast Received !", Toast.LENGTH_SHORT).show();
Log.d("KEYY","BROADCAST RECEIVED !");

}
}

Step 2] Inside the Main Activity or anywhere inside the App , form where you want to send the broadcast , create a EXPLICIT intent. 

Pass the intent object , the context and the Broadcastreceiverr class you want to send broadcast too.

then use the sendBroadcast()


//user clicks on button to execute this method
public void SendBroadcast(View view) {

//creating explicit intent
Intent intent = new Intent(this,Broadcatsreceiverrr.class);

//Send the broadcast.
sendBroadcast(intent);
}



Step 3] Now we need to register the Broadcastreceiver class as receiver inside the Manifest file.

(If you dont do this your broadcats wont be received)


 <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"
tools:ignore="ExtraText">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//register your broadcatsreciver class a receiver
<receiver android:name=".Broadcatsreceiverrr"/>
</application>

</manifest>


Thats it.


---------------------------------------------------------------------------------------------------


















































Comments

Popular posts from this blog

React Js + React-Redux (part-2)

React Js + CSS Styling + React Router (part-1)

ViteJS (Module Bundlers, Build Tools)