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...

Handler in Android

Need to notify user when for example user clicks button and in onClick method program starts some long-time process. Need to have some solution which will do your task in background. Handler is very good solution for this problem.
If you will not use such approach then user will see that program appears to hang.
                             
                   When you create an object from the Handler class, it processes
Messages and Runnable objects associated with the current thread MessageQueue. the message queue holds the tasks to be executed in FIFO (First In First Out) mannser. you will need only ine Handler per activity where the background thread will communicate with to update the UI.
The Handler is associated with the thread from which it’s been created
We can communicate with the Handler by two methods:
  1. Messages.
  2. Runnable objects.
I am going to show you using runnable Object..

public class Expense extends Activity {

   
    Handler handler = new Handler();
   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expense);
        ctx = this;
       handler.post(message);
}
 private Runnable message = new Runnable() {

        //@Override
        public void run() {
          
     //do task needed
       
                      
        }
    };
}








Sending Messages in a timely manner:

we can use handlers to send messages or post runnables at time intervals using
The following methods:
  1. handler.sendEmptyMessageAtTime(int what,long uptimeMillis):sends an
    empty message at a specific time in milli-seconds, can be defined by using the
    SystemClock.uptimeMillis() method to get the time since the device boot
    in milli-seconds and concatinating to it.
  2. handler.sendEmptyMessageDelayed(int what,long delayMillis):sends an
    empty message after a certain amount of time in milli-seconds.
  3. handler.sendMessageAtTime(Message msg,long uptimeMillis).
  4. handler.sendMessageDelayed(Message msg,long delayMillis).
  5. handler.postAtTime(Runnable r,long uptimeMillis).
  6. handler.postAtTime(Runnable r,Object token,long uptimeMillis):posts a
    runnable with an object as a distinguishing token.
  7. handler.postDelayed(Runnable r,long delayMillis)

Removing Call backs:

If you want to remove a runnable or a message from the message queue, you can use the following methods:
  1. handler.removeCallbacks(Runnable r).
  2. handler.removeCallbacks(Runnable r,Object token).
  3. handler.removeCallbacksAndMessages(Object token).
  4. handler.removeMessages(int what).
  5. handler.removeMessages(int what,Object object)

    suppose if you want to execute block of statement at regular interval.laet say after every 10 seconds..

public class Expense extends Activity {

   
    Handler handler = new Handler();
   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expense);
        ctx = this;
       handler.post(message);
}
 private Runnable message = new Runnable() {

        //@Override
        public void run() {
          
     //do task needed
       
            handler.postDelayed(message,10000)  ;        
        }
    };
}

     

Comments

Popular posts from this blog

Webview data directory for Android 9 (Pie)

Tabs in Android

DatePicker in Android