Android badge icon

Frequently you see Mail, or Phone icon of your home screen displays a red balloon. In the balloon, you see a number indicating unread messages, or missed calls. This is called a badge. In this Android tip, I am going to show you how to add a badge to the icon of an application.
In Android, you can achieve this goal simply. First, you need to create an Intent object for the android.intent.action.BADGE_COUNT_UPDATE action. Then you use the putExtra() method to add extended data such as badge_count, badge_count_package_name, and badge_count_class_name to the Intent object. The badge_count key represents a number to display in the red balloon. The badge_count_package_name key specifies the package name of your application. You can get the package name by using the getPackageName() method. The badge_count_class_name key specifies the name of class that will be opened when the application launches. Usually, it is the main class of your application. Finally, you call the sendBroadcast() method with passing the Intent object to add the badge to the application icon. You should note that if you set zero to the badge_count, the badge will be removed from the icon.
Here is the code to add a badge to the application icon when the application launches.




import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity{

   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);    
     addBadge(this, 3);
   }
  
  
   private void addBadge(Context context, int count) {
   Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
     intent.putExtra("badge_count", count);
     intent.putExtra("badge_count_package_name", context.getPackageName());
     intent.putExtra("badge_count_class_name", this.getClass().getName());
     context.sendBroadcast(intent);
   }
}

Android-badge-icon

Posted by: Dara | post date: 12-24-2014 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.