Practical No 21
Practical No 21
21
2.
A broadcast receiver has single callback method:
void onReceive(Context curContext, Intent broadcastMsg)
3.
In android, several system events are defined as final static fields in the Intent class. Following
are the some of system events available in android applications.
Event Description
android.intent.action.BOOT_COMPLETED It raise an event, once boot completed.
android.intent.action.POWER_CONNECTED It is used to trigger an event when power
connected to the device.
android.intent.action.POWER_DISCONNECTED It is used to trigger an event when power
got disconnected from the device.
android.intent.action.BATTERY_LOW It is used to call an event when battery is
low on device.
android.intent.action.BATTERY_OKAY It is used to call an event when battery is
OKAY again.
android.intent.action.REBOOT It call an event when the device rebooted
again.
X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to demonstrate all the system broadcast messages.
package com.jamiapolytechnic.brdcstrcvrex;
import android.app.Activity;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
this.registerReceiver(receiver, intentFilter);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
EditText st = (EditText)findViewById(R.id.txtMsg);
Intent intent = new Intent();
intent.setAction("com.jamiapolytechnic.CUSTOM_ACTION");
intent.putExtra("msg",(CharSequence)st.getText().toString());
sendBroadcast(intent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
//=====================================================================
//MyBroadcastReceiver.java
package com.jamiapolytechnic.brdcstrcvrex;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.Toast;
//=====================================================================
//activity_main.xml
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickShowBroadcast"
android:layout_marginLeft="130dp"
android:text="Show Broadcast"/>
</LinearLayout>
//=====================================================================
//AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BrdCstRcvrEx">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.jamiapolytechnic.CUSTOM_ACTION" />
</intent-filter>
</receiver>
</application>
</manifest>
Dated signature of
Marks Obtained
Teacher
Process Product Total
Related(10) Related(15) (25)