Android Broadcast

There are two ways to Create Broadcast in Android
  1. Static Broadcast .
  2. Dynamic Broadcast . 

1 Static Broadcast


1.a XML Layout

file : activity_main.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tutorialsbuzz.broadcastdemo.MainActivity"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BroadCast"
        android:onClick="btnClick" />
  
</LinearLayout>

1.b Broadcast

file : MyBroadcast.java
package com.tutorialsbuzz.broadcastdemo;

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

public class MyBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Static Broadcast Received", Toast.LENGTH_LONG)
                .show();
    }

}


1.c Activity

file : MainActivity.java
package com.tutorialsbuzz.broadcastdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void btnClick(View view) {
        Intent intent = new Intent();
        intent.setAction("com.tutorialsbuzz.customReceiver");
        sendBroadcast(intent);
    }

}


1.d Android Manifest

file : AndroidManifest.xml
<receiver android:name=".MyBroadcast" >
    <intent-filter>
        <action android:name="com.tutorialsbuzz.customReceiver" />
    </intent-filter>
</receiver>


2. Dynamic Broadcast


2.a XML Layout

file : activity_main.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tutorialsbuzz.broadcastdemo.MainActivity"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BroadCast"
        android:onClick="btnClick" />
  
</LinearLayout>

2.b Broadcast

file : MyBroadcast.java
package com.tutorialsbuzz.dynamicbroadcastdemo;

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

public class MyBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Dynamic Broadcast Received", Toast.LENGTH_LONG)
                .show();
    }

}


2.c Activity

file : MainActivity.java
package com.tutorialsbuzz.dynamicbroadcastdemo;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    MyBroadcast receiver;
    static final public String BROADCAST_ACTION = "com.tutorialsbuzz.customReceiver";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        receiver = new MyBroadcast();
    }

    public void btnClick(View view) {
        Intent intent = new Intent();
        intent.setAction(BROADCAST_ACTION);
        sendBroadcast(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter mfilter = new IntentFilter(BROADCAST_ACTION);
        registerReceiver(receiver, mfilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);

    }
}


2.d Android Manifest

file :AndroidManifest.xml
<receiver android:name=".MyBroadcast" > </receiver>



No comments:

Post a Comment