Broadcast Receivers are used to respond to the other’s applications broadcast messages and messages are called Intents or Events. For Example , other applications in the android device can be informed via Broadcast messages that some of the data has been downloaded to the android device and is available to use for them , so in the meanwhile broadcast receiver will track down the message and will take down appropriate actions.
Steps for Evaluation
____________________________________________
Step 1 : Create a new project named as “com.YOUR_NAME.android.alarm” with the activity “Alarm-Activity“. Create the following layout.
____________________________________________
<?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"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/time_sec" android:hint="Numbofsecs" android:inputType="numberDecimal"></EditText><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ok1" android:onClick="alert_start" android:text="Initiate Counter"></Button> </LinearLayout>
____________________________________________
Step 2 : Now its time to create the below broadcast receiver class which will get the Vibrator service.
____________________________________________
package com.YOUR_NAME.android.alarm; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Vibrator; import android.widget.Toast; public class YourBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Sorry ! your time is out.", Toast.LENGTH_LONG).show(); // To vibrate the cellphone Vibrator vibrator_1 = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibrator_1.vibrate(3000); } }
____________________________________________
Step 3 : This class should be maintained like broadcast receiver within “AndroidManifest.xml” and vibration authorization should be allowed.
____________________________________________
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.YOUR_NAME.android.alarm" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Alarm-Activity" 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=".YourBroadcastReceiver" android:enabled="true"> </receiver> </application> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.VIBRATE"></uses-permission> </manifest>
____________________________________________
Step 4 : Its time to create your Main Activity which will create an intent for Broad-Cast-Reciever in order to get reciever Alarm-Manager Service .
____________________________________________
package com.YOUR_NAME.android.alarm; 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 Alarm-Activity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void alert_start(View view) { EditText text_1 = (EditText) findViewById(R.id.time_sec); int integer = Integer.parseInt(text_1.getText().toString()); Intent intent_1 = new Intent(this, YourBroadcastReceiver.class); PendingIntent pending_Intent = PendingIntent.getBroadcast( this.getApplicationContext(), 234324243, intent_1, 0); AlarmManager alarmManager_1 = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager_1.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pending_Intent); Toast.makeText(this, "Set in Alarm " + integer + " secs", Toast.LENGTH_LONG).show(); } }