Write a Program to demonstrate the example of Broadcast Receiver.

 XML FILE

<RelativeLayout 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=".MainActivity">

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Example of Broadcast"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="290dp"

        android:fontFamily="sans-serif-black"

        android:textSize="30dp" />


 <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/button2"

        android:text="Broadcast Intent"

        android:onClick="broadcastIntent"

        android:layout_below="@id/textView1"

        android:layout_centerHorizontal="true" />


</RelativeLayout>



CODE :

MainActivity.java

package com.example.myapplication;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

public class MainActivity extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

    public void broadcastIntent(View view){

        Intent intent = new Intent();   intent.setAction("com.example.myapplication.CUSTOM_INTENT"); sendBroadcast(intent);

    }

}

MyReciever.java

package com.example.myapplication;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;


public class MyReceiver extends BroadcastReceiver{

    @Override

    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Intent Detected By Zainab.", Toast.LENGTH_LONG).show();

    }

}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.myapplication">


    <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>

        <receiver android:name="MyReceiver">

            <intent-filter>

                <action android:name="com.example.myapplication.CUSTOM_INTENT">

                </action>

            </intent-filter>


        </receiver>

    </application>

</manifest>


Comments

Popular posts from this blog

Write a program to create a first display screen on any search engine using auto complete text View

Write a Program to create a toggle button to display the ON/OFF Bluetooth on the display screen.

Write a program to create a login form for social networking website.