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

4 comments:

  1. I have completely read your post and the content is crisp and clear. Thank you for posting such an informative article, I have decided to follow your blog so that I can myself updated.Android Training in Bangalore

    ReplyDelete
  2. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it... Java Training in Chennai | Python Training in Chennai

    ReplyDelete
  3. 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