Skip to content

Commit c4f3343

Browse files
committed
Added explicit broadcast receivers and fixed a bug in dynamic receivers with initalStickyBroadcast
1 parent 1d9285f commit c4f3343

File tree

7 files changed

+73
-10
lines changed

7 files changed

+73
-10
lines changed

BroadcastReceivers/.idea/gradle.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BroadcastReceivers/app/src/main/AndroidManifest.xml

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
</intent-filter>
3535
</receiver>
3636

37+
<receiver android:name=".receivers.StaticExplicit_4_BroadcastReceiver"
38+
android:exported="true">
39+
</receiver>
40+
3741
</application>
3842

3943
</manifest>

BroadcastReceivers/app/src/main/java/in/rafique/androidbasics/broadcastreceivers/MainActivity.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import androidx.appcompat.app.AppCompatActivity;
44

5+
import android.content.ComponentName;
56
import android.content.Intent;
67
import android.content.IntentFilter;
78
import android.net.ConnectivityManager;
@@ -29,7 +30,7 @@ protected void onStart() {
2930

3031
//Registering the second receiver
3132
IntentFilter filter = new IntentFilter() ;
32-
filter.addAction("android.media.RINGER_MODE_CHANGED"); // you can add multiple action here i.e. listen to multiple intent filters
33+
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); // you can add multiple action here i.e. listen to multiple intent filters
3334
registerReceiver(dynamicImplicit2NetworkReceiver, filter) ;
3435

3536

@@ -59,4 +60,15 @@ public void onClick_Btn3_sendCustomBroadCast(View v){
5960

6061

6162
}
63+
64+
public void onClick_Btn4_ExplicitBroadcastReceiver(View v){
65+
String packageName = "in.rafique.androidbasics.broadcastreceivers" ;
66+
String broadcastReceiverFullName = "in.rafique.androidbasics.broadcastreceivers.receivers.StaticExplicit_4_BroadcastReceiver" ;
67+
// when we do new Intent(context, className.java) we are stll doing the following thing
68+
// the context gives package name, and the class name gives class path
69+
ComponentName componentName = new ComponentName(packageName, broadcastReceiverFullName) ;
70+
Intent intent = new Intent() ;
71+
intent.setComponent(componentName) ;
72+
sendBroadcast(intent);
73+
}
6274
}

BroadcastReceivers/app/src/main/java/in/rafique/androidbasics/broadcastreceivers/receivers/DynamicImplicit_2_NetworkBroadcastReceiver.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@
1010
public class DynamicImplicit_2_NetworkBroadcastReceiver extends BroadcastReceiver {
1111
@Override
1212
public void onReceive(Context context, Intent intent) {
13-
if("android.media.RINGER_MODE_CHANGED".equals(intent.getAction())){
14-
Log.d("jdfslj =>", "I am inside onReceive") ;
15-
Toast.makeText(context, "Ringer Changed", Toast.LENGTH_SHORT).show();
13+
// Registering Dynamically sends an initial sticky broadcast, every time app resumes, opens
14+
// we don't want that, so bypassing the initial-sticky-broadcast here using isInitialStickyBroadcast()
15+
if(!isInitialStickyBroadcast()){
16+
17+
18+
if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())){
19+
boolean hasNoConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false) ;
20+
if(hasNoConnectivity){
21+
Toast.makeText(context, "NOOO, network is gone my lord", Toast.LENGTH_SHORT).show();
22+
}else{
23+
Toast.makeText(context, "YAYYY, the network is back my Lord", Toast.LENGTH_SHORT).show();
24+
}
25+
}
1626
}
27+
1728
}
1829
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package in.rafique.androidbasics.broadcastreceivers.receivers;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.widget.Toast;
7+
8+
public class StaticExplicit_4_BroadcastReceiver extends BroadcastReceiver {
9+
@Override
10+
public void onReceive(Context context, Intent intent) {
11+
// i don't right now know when to use explicit broadcast receiver
12+
Toast.makeText(context, "This is from explicit broadcast receiver", Toast.LENGTH_SHORT).show();
13+
}
14+
}

BroadcastReceivers/app/src/main/res/layout/activity_main.xml

+26-6
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,53 @@
5757
android:layout_width="wrap_content"
5858
android:layout_height="wrap_content"
5959
android:orientation="horizontal"
60-
android:padding="16dp"
6160
>
6261
<Button
6362
android:layout_width="wrap_content"
6463
android:layout_height="wrap_content"
6564
android:text="Send Custom Broadcast"
65+
android:textSize="8sp"
6666
android:onClick="onClick_Btn3_sendCustomBroadCast"
67+
android:layout_marginTop="16dp"
6768
/>
6869

70+
<TextView
71+
android:layout_width="wrap_content"
72+
android:layout_height="wrap_content"
73+
android:text="3. This button sends a custom broadcast with intent filter of yoloman.super.duper.myFilter.MY_ACTION . I have also declared a broadcast receiver that takes in this intent filter and can catch that. So when you click this button, my receiver will catch that braodcast and will show a toast message"
74+
android:layout_gravity="top"
75+
android:gravity="top"
76+
android:padding="8dp"
77+
android:layout_margin="8dp"
78+
/>
79+
80+
6981
</LinearLayout>
7082

7183
<LinearLayout
7284
android:layout_width="wrap_content"
7385
android:layout_height="wrap_content"
7486
android:orientation="horizontal"
75-
android:padding="16dp"
7687
>
7788
<Button
7889
android:layout_width="wrap_content"
7990
android:layout_height="wrap_content"
80-
android:text="Open MyJobIntentService"
81-
android:onClick="openMyJobIntentService"
82-
android:layout_margin="16dp"
91+
android:text="Explicit Broadcast"
92+
android:onClick="onClick_Btn4_ExplicitBroadcastReceiver"
93+
android:layout_marginTop="16dp"
94+
android:textSize="8sp"
8395
/>
8496

8597

86-
98+
<TextView
99+
android:layout_width="wrap_content"
100+
android:layout_height="wrap_content"
101+
android:text="4. This button explicitly calls the broadcast receiver by passing the package and the class name. You can also do this for calling broadcast receivers of different apps by passsing their package name and their broadcast receiver name. just make sure that the receiver in the manifest is exported=true because when you dont put intent filter, the value of exported becomes false"
102+
android:layout_gravity="top"
103+
android:gravity="top"
104+
android:padding="8dp"
105+
android:layout_margin="8dp"
106+
/>
87107

88108

89109
</LinearLayout>

Service/.idea/gradle.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)