Created
April 10, 2023 16:33
-
-
Save biswa-rx/7f0e26536af60e3dca3a9e5ffdbd0852 to your computer and use it in GitHub Desktop.
BroadcastReceiver Tutorial Part 3
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.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); | |
IntentFilter filter = new IntentFilter("com.codinginflow.EXAMPLE_ACTION"); | |
registerReceiver(exampleBroadcastReceiver, filter); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
unregisterReceiver(exampleBroadcastReceiver); | |
} | |
} |
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.widget.Toast; | |
public class ExampleBroadcastReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if ("com.codinginflow.EXAMPLE_ACTION".equals(intent.getAction())) { | |
String receivedText = intent.getStringExtra("com.codinginflow.EXTRA_TEXT"); | |
Toast.makeText(context, receivedText, 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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" | |
android:orientation="vertical" | |
android:padding="16dp" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/text_view" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Message" | |
android:textSize="20sp" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:onClick="sendBroadcast" | |
android:text="Send Broadcast" /> | |
</LinearLayout> |
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.broadcastsender; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
private TextView textView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
textView = findViewById(R.id.text_view); | |
} | |
public void sendBroadcast(View v) { | |
Intent intent = new Intent("com.codinginflow.EXAMPLE_ACTION"); | |
intent.putExtra("com.codinginflow.EXTRA_TEXT", "Broadcast received"); | |
sendBroadcast(intent); | |
} | |
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
String receivedText = intent.getStringExtra("com.codinginflow.EXTRA_TEXT"); | |
textView.setText(receivedText); | |
} | |
}; | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
IntentFilter filter = new IntentFilter("com.codinginflow.EXAMPLE_ACTION"); | |
registerReceiver(broadcastReceiver, filter); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
unregisterReceiver(broadcastReceiver); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment