Created
April 10, 2023 16:22
-
-
Save biswa-rx/26d81fd6fe4590869c98a2990e307f34 to your computer and use it in GitHub Desktop.
BroadcastReceiver Tutorial Part 2
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.codinginflow.broadcastexample"> | |
<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"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
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.codinginflow.broadcastexample; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.net.ConnectivityManager; | |
import android.widget.Toast; | |
public class ExampleBroadcastReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { | |
boolean noConnectivity = intent.getBooleanExtra( | |
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false | |
); | |
if (noConnectivity) { | |
Toast.makeText(context, "Disconnected", Toast.LENGTH_SHORT).show(); | |
} else { | |
Toast.makeText(context, "Connected", Toast.LENGTH_SHORT).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
package com.codinginflow.broadcastexample; | |
import android.content.IntentFilter; | |
import android.net.ConnectivityManager; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
public class MainActivity extends AppCompatActivity { | |
ExampleBroadcastReceiver exampleBroadcastReceiver = new ExampleBroadcastReceiver(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); | |
registerReceiver(exampleBroadcastReceiver, filter); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
unregisterReceiver(exampleBroadcastReceiver); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment