Users Online

· Guests Online: 89

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

040 Java Android Program to Demonstrate BroadCast Receiver to Intercept Custom Intent

ava Android Program to Demonstrate BroadCast Receiver to Intercept Custom Intent

This Android Java Program demonstrates a BroadCast Receiver to Intercept Custom Intent.

 

Here is source code of the Program demonstrate a BroadCast Receiver to Intercept Custom Intent in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

So far, in the previous posts we have looked at using Intents to start new application components, but you can also use Intents to broadcast messages anonymously between components via the sendBroadcast() method. As a system-level message-passing mechanism, Intents are capable of sending structured messages across process boundaries. As a result, you can implement Broadcast Receivers to listen for, and respond to, these Broadcast Intents within your applications.

Broadcasting Intents helps make your application more open; by broadcasting an event using an Intent, you let yourself and third-party developers react to events without having to modify your original application. Within your applications you can listen for Broadcast Intents to to react to device state changes and third-party application events.
Android uses Broadcast Intents extensively to broadcast system events, such as changes in network connectivity, docking state, and incoming calls etc.

BroadCast_intent

Below is the given code for BroadCast Receiver to Intercept Custom Intent.

Main Activity

 
package com.example.custom_broadcast_intent;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1 = (Button)findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
             broadcastintent();    
            }
        });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    public void broadcastintent(){
        Intent intent = new Intent();
        intent.setAction("com.example.custom_broadcast_intent.MyReceiver");
        sendBroadcast(intent);
    }
 
}

Myreceiver

package com.example.custom_broadcast_intent;
 
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) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
    }
 
}

Activity_Main.xml

 
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="190dp"
        android:text="BroadCast Intent" />
 
</RelativeLayout>

You need register the receiver in Android Manifest.

Android Manifest

 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.custom_broadcast_intent"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.custom_broadcast_intent.MainActivity"
            android:label="@string/app_name" >
            <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.custom_broadcast_intent.MyReceiver" >
                </action>
            </intent-filter>
        </receiver>
    </application>
 
</manifest>

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.70 seconds
10,823,748 unique visits