Tuesday, January 22, 2013

Android BoradCast Reciever

Broadcast Receiver :


A broadcast receiver listens for relevant broadcast messages to trigger an event. Some examples of broadcasted events already sent from the OS are
> The camera button was pressed.
> The battery is low.
> A new application was installed.
A user-generated component can also send a broadcast, such as:
> A calculation was finished.
> A particular thread has started.

make a project with the name SimpleBoradcastReciever


src/com/jitesh/simplebroadcastreceiverer/SimpleActivity.java




package com.jitesh.simplebroadcastreceiver;


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

public class SimpleActivity extends Activity {
    SimpleBroadcastReceiver intentReceiver =
        new SimpleBroadcastReceiver();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        IntentFilter intentFilter =
            new IntentFilter(Intent.ACTION_CAMERA_BUTTON);
        intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        registerReceiver(intentReceiver, intentFilter);

    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(intentReceiver);
        super.onDestroy();
    }  
}

src/com/jitesh/simplebroadcastreceiverer/SimpleBroadcastReceiver .java


package com.jitesh.simplebroadcastreceiver;


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

public class SimpleBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context rcvContext, Intent rcvIntent) {
        String action = rcvIntent.getAction();
        if (action.equals(Intent.ACTION_CAMERA_BUTTON)) {
            rcvContext.startService(new Intent(rcvContext,
                    SimpleService2.class));
        }
    }
}


src/com/jitesh/simplebroadcastreceiverer/SimpleService2  .java


package com.jitesh.simplebroadcastreceiver;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class SimpleService2 extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this,"Service created ...",
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed ...",
                Toast.LENGTH_LONG).show();      
    }
}


main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>


manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jitesh.simplebroadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SimpleActivity"
            android:label="@string/title_activity_simple" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".SimpleService2" >
        </service>

        <receiver
            android:name=".SimpleBroadcastReceiver"
            android:enabled="true" >
        </receiver>
    </application>

</manifest>


1 comment:

  1. Hi AAREN thanks for the appreciation. you can reach me at upadhyay.jitesh@gmail.com and please share the knowledge.

    ReplyDelete