Simple Notification in Android

Image
Notifications are a way to provide information to your customer which displays as a pop up on the device. It helps you to keep in touch with your customer. I am going to create a simple notification without going deep because everything is available on Google. We must understand a few concepts before going further. Notification channel:  Assume you have developed an app for an online learning center where they teach Maths and English. Students can either learn Maths, English, or Both. we must send notification related to Maths to only students who have taken Maths. In this case, we can create channels for Maths, English, and both so triggered notification will be delivered to students who have taken that subject. Each Notification channel can have a different Notification sound and different behavior that you can change from the Notification setting page. I forgot to tell you that Google has introduced the Notification channel from Android 8.0 i.e API level 26. Now yo...

AsyncTask in Android

In Android, Services are used to keep running the application in background..If You want to small task in background then what you will do ??. In that case you have to use AsyncTask.
                     If your appication requires connection with server,loading and uploading data to server etc. In such cases,your appication becomes more complex.You can solve this by using AsyncTask.You can show a user some kind of notification or progressdialog box to display something on main screen while loading data from server in background without interuppting the main thread.

        
                       AsyncTask is an abstract class that provides several methods managing the interaction between the UI thread and the background thread. it’s implementation is by creating a sub class that extends AsyncTask and implementing the different protected methods it provides.

We are now going to create AsyncTasj sub class by like this:

class ServerTask extends AsyncTask<Params,Progress,Result>
{
}

It contains three parameter:
  1. Params: parameter info passed to be used by the AsyncTask.
  2. Progress: the type of progress that the task accomplishes.
  3. The result returned after the AsyncTask finishes.
Next step is we have to override the protected methods of AsyncTask class that defined its LifeCycle.

Methods:
  1. onPreExecute: the first method called in the AsyncTask, called on the UI thread.
  2. doInBackground: the method that executes the time consuming tasks and publish the task progress, executed in background thread.
  3. onProgressUpdate: method that updates the progress of the AsyncTask, run on the UI thread.
  4. onPostExecute: the final method that gets called after doInBackground finishes, here we can update the UI with the results of the AsyncTask.
  5. onCancelled: gets called if the AsyncTask.cancel() methods is called, terminating the execution of the AsyncTask.

    Example:
     public class Details extends Activity {
      
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.details);
            ctx =this;
          
            
             readWebpage();
        }
      
      
      
        private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... urls) {
     
                for (String url : urls) {
              
            //    do background task

                }
                return String();
              
            }
          
            @Override
            protected void onPreExecute() {
              
                 ploading = ProgressDialog.show(ctx,"" ,"");
                 ploading.setContentView(R.layout.wheel);
                 ploading.setCancelable(true);
            }

            @Override
            protected void onPostExecute(String result) {
          
              
              
                ploading.dismiss();
                //set the loaded data in your ui
              
                }

            }
          
        }
        public void readWebpage() {
            DownloadWebPageTask task = new DownloadWebPageTask();
            task.execute(new String[] { "http://"www.arjun30.blogspot.in"});

        }
      

    }




     

    Comments

    Popular posts from this blog

    Webview data directory for Android 9 (Pie)

    Tabs in Android

    DatePicker in Android