Notes AsyncTask class

Download 39 Kb.
Size39 Kb.
AsyncTask class.
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. 


  1. Create a new project File New Android Project and give activity name asAndroidMenusActivity.

  2. Now create an XML file under res/layout folder and name it as menu.xml.

  3. Open menu.xml file and type following code. In the following code we are creating a single menu with 6 menu items. Each menu item has an icon and title for display the label under menu icon. Also we have id for each menu item to identify uniquely.


        Set id, icon and Title for each menu item




          android:title="Bookmark" />


          android:title="Save" />




          android:title="Search" />



     * Event Handling for Individual menu item selected

     * Identify single menu item by it's id

     * */

public boolean onOptionsItemSelected(MenuItem item)



        switch (item.getItemId())



            // Single menu item is selected do something

            // Ex: launching new activity/screen or show alert message

            Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();

            return true;



            Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();

            return true;



            Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();

            return true;



            return super.onOptionsItemSelected(item);


//play an audio file
public class MainActivity extends Activity {  



    protected void onCreate(Bundle savedInstanceState) {  




        MediaPlayer mp=new MediaPlayer();  


            mp.setDataSource(“song.mp3");//Write your location here  




        }catch(Exception e){e.printStackTrace();}  



Broadcast receiver.
Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action. Two main actions are :

  • Creating the Broadcast Receiver

  • A broadcast receiver is implemented as a subclass of BroadcastReceiverclass and overriding the onReceive() method where each message is received as a Intent object parameter.

public class MyReceiver extends BroadcastReceiver {


public void onReceive(Context context, Intent intent) {

Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();



  • Registering Broadcast Receiver

  • An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest.xml file. Consider we are going to registerMyReceiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.



android:theme="@style/AppTheme" >

    • Now whenever your Android device gets booted, it will be intercepted by BroadcastReceiver MyReceiver and implemented logic inside onReceive() will be executed.


Download 39 Kb.

Share with your friends:

The database is protected by copyright © 2024
send message

    Main page