Monday, 31 October 2016

BroadCast Receiver

 BroadCast Receiver is also called as "Communicate Collector". It is a component.

A communicate collector is an Android segment which permits to enlist and listen for gadget introduction changes like sms message received , telephone call received/pick/cut ,battery status changed, the Wi-Fi went ahead.

Android working framework and even different applications time to time communicate messages about things that are occurring like sms messsage recieved , telephone call recieved/pick/cut ,battery status changed, the Wi-Fi went ahead.

With the assistance of communicate recipient you will find Android working framework particular occasions and after that you can code your application for that occasion.

Example :-
                      Your Battery Getting low.
                      Your Phone has suddenly Turn off.
                      Your Battery 10% to 20% remaining.
                      Image Capture also broadcast receiver.

Syntax :-
                In android mainfest.xml,

                   <receiver android:name="MainActivity">  
                   <intent-filter>
                    <action android:name="com.kiranapp.BroadCast Receiver">
                    </action>
                    </intent-filter>  
                  </receiver>

          In MainActivity.java,

               public class MainActivity extends Broadcast Receiver
              {
                 public void onReceive(Context context, Intent intent)
                  {
                  }
              }
   
  Example Code for BroadCast Receiver  shownbelow.

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?> 
<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" 
 android:orientation="vertical" 
 tools:context="com.kiranapp.MainActivity" 
 android:background="#a69090">
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Battert status is : " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" 
 android:background="#f3ecec" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

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.widget.TextView;

public class MainActivity extends AppCompatActivity {
 TextView t1;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 t1=(TextView)findViewById(R.id.textView);
 registerReceiver(new MyBro(),new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
 }
class MyBro extends BroadcastReceiver{
@Override 
 public void onReceive(Context context, Intent intent) {
 int bstatus=intent.getIntExtra("level",0);
 t1.setText("Battery Status is : "+bstatus);
 }
 }
}

Output :-

BroadCast Receiver
BroadCast Receiver Figure

6 comments:

  1. Thank you for this brief explanation and very nice information. Well, got a good knowledge. Java Training in Chennai | Blue prism Training in Chennai

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. A great content material as well as great layout. Your website deserves all of the positive feedback it’s been getting. I will be back soon for further quality contents. 해외스포츠중계

    ReplyDelete
  4. Great explanation! Your detailed breakdown of Broadcast Receivers, along with the example code, makes it easy to understand how to use them in Android development. Thanks for sharing!
    cyber security internship for freshers | cyber security internship in chennai | ethical hacking internship | cloud computing internship | aws internship | ccna course in chennai | java internship online

    ReplyDelete