Notes AsyncTask class



Download 39 Kb.
Date04.07.2017
Size39 Kb.
#22461
Notes
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. 

Menu


  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:icon="@drawable/icon_bookmark"

          android:title="Bookmark" />


              android:icon="@drawable/icon_save"

          android:title="Save" />

 

   

          android:icon="@drawable/icon_search"

          android:title="Search" />



//MainActicity.java

/**


     * Event Handling for Individual menu item selected

     * Identify single menu item by it's id

     * */
 

public boolean onOptionsItemSelected(MenuItem item)

    {

         



        switch (item.getItemId())

        {

        case R.id.menu_bookmark:

            // 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;

 

        case R.id.menu_save:



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

            return true;

 

        case R.id.menu_search:



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

            return true;

 

default:


            return super.onOptionsItemSelected(item);

        }




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

  

    



    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

          

        MediaPlayer mp=new MediaPlayer();  

        try{  

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

            mp.prepare();  

            mp.start();  

              

        }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 {

@Override

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:icon="@drawable/ic_launcher"

android:label="@string/app_name"

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 ©ininet.org 2024
send message

    Main page