BroadCastReceiver:
BroadCastReceiver are the Receiver which
can respond to the messages which are register by the System Or other Application.BroadCast is Nothing but just Broadcasting what ever register by the System.
For Example : Movies are Produce and Directed by Others and give the final copy of the movie to the Theatre Owners and the Owner will Broadcast that Movie.
How To Register BroadCastReceiver:
There are two ways to register Android broadcastreceiver
I.One
is static way in which the broadcast receiver is registered in an android
application via AndroidManifest.xml file.
II.Another
way of registering the broadcast receiver is dynamic, which is done using
Context.registerReceiver() method.Dynamically registered broadcast
receivers can be unregistered using Context.unrgisterReceiver() method.
1. A broadcast receiver is an Android component which allows to
register and listen for device orientation changes like sms messsage recieved ,
phone call recieved/pick/cut ,battery status changed, the Wi-Fi came on.
2. Android operating system and even other applications time to time broadcast messages about things that are happening like sms messsage recieved , phone call recieved/pick/cut ,battery status changed, the Wi-Fi came on.
3. With the help of broadcast receiver you will catch Android operating system specific events and then you can code your application for that event.
How to use BroadCastReceiver:
Example:
<receiver Broandroid:name="IncomingCall">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter> </receiver>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter> </receiver>
Create Android Application:
- File >> New >> Android
Application
- Enter Project
name: BroadCastApp
- Package: com.ambilpursunil.newapp
- Keep other
default selections, click Next until you reach Finish
1.Simply Copy and Past the Code which is display below in the MainActivity.java
package com.ambilpursunil.newpp;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startAlert (View view){
EditText text = (EditText) findViewById(R.id.time);
int i =Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(),234324243,
intent,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(alarmManager.RTC_WAKEUP,System.currentTimeMillis() + (i*1000),pendingIntent);
Toast.makeText(this,"Alarm
Set is:" + i + "Seconds", Toast.LENGTH_LONG).show();
}
}
2. Now Create BroadCastReceiver Class, By Right clicking on the src=>New=>Java Class=>Name it as MyBroadcastReceiver.java Copy and Past The Below Code.
package com.ambilpursunil.newpp;
import
android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context,
Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context,"Dont
Panic Just Alarm", Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator)
//Registering BroadCastReceiver
context.getSystemService(Context.VIBRATOR_SERVICE);
//Registering BroadCastReceiver
context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
}
}
3.Simple Copy and Past the below code: actvitiy_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="com.ambilpursunil.newapp.MainActivity" >
<EditText android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Numbe Of Secondsr"
android:inputType="numberDecimal"/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/time"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:onClick="startAlert"
android:text="Start Alarm" />
</RelativeLayout>
4.Copy and past the code for manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ambilpursunil.newapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21"
/>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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="com.ambilpursunil.newapp.MyBroadcastReceiver"> </receiver>
</application>
</manifest>
5.Right click on the project and Run As-->Android Application
OutPut:
Once Application Launch then you will see a EditText Edit Text It Will ask You To Enter Number of seconds to set the alarm enter 10 seconds and click on the StartAlarm Button you will get a Toast Message lik Don't Panic Just Alarm. as OUTPUT.
Please Send Your Comments To ambilpura.sunil@gmail.com
Stay Tune For Next Tutorial... Phone Call In Android:
No comments:
Post a Comment