Friday, 28 February 2014

Notification Example in Android

Hello Guys!!! Hope you all doing well...
Notification in android and its type going to discuss here.
A notification is a user interface element that you display outside your app's normal UI to indicate that an event has occurred.
 Examples in this tutorial  are based on the NotificationCompat.Builder class. NotificationCompat.Builder is in the Support Library named as android-support-v4.jar . This jar file is avilable in our android Project libs folder. We should use NotificationCompat and its subclasses, particularly NotificationCompat.Builder, to provide the best notification support for a wide range of platforms.
Now the first thing we have to do is "Create a Notification Builder":--
When creating a notification, specify the UI content and actions with a NotificationCompat.Builder object. At bare minimum, a Builder object must include the following:

  • A small icon, set by setSmallIcon()
  • A title, set by setContentTitle()
  • Detail text, set by setContentText()
Here is the Example:- 
NotificationCompat.Builder mBuilder new NotificationCompat.Builder(this)
   
.setSmallIcon(R.drawable.notification_icon)
   
.setContentTitle("My notification")
   
.setContentText("Hello World!");

After creating notification Builder we have to define "Notification's Action". An action takes users directly from the notification to an Activity in your application, where they can look at the event that caused the notification or do further work. Inside a notification, the action itself is defined by a PendingIntent containing an Intent that starts an Activity in your application.
How you construct the PendingIntent depends on what type of Activity you're starting. When you start an Activity from a notification, you must preserve the user's expected navigation experience. Here is example
Intent resultIntent = new Intent(this, ResultActivity.class);
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,
                                           resultIntent,
                                          PendingIntent.FLAG_UPDATE_CURRENT );
So after defining Notification action you have to set it in Notification builder for  starting that activity. For example, to start an activity when the user clicks the notification text in the notification drawer, add the PendingIntent by calling setContentIntent()

mBuilder.setContentIntent(resultPendingIntent);
Now the last thing you  have to issue that notification  you created. To issue the notification Do following :-

  • Get an instance of NotificationManager.
  • Use the notify() method to issue the notification. When you call notify(), specify a notification ID. You can use this ID to update the notification later on.
  • Call build(), which returns a Notification object containing your specifications.
Example:-

// Sets an ID for the notification
int mNotificationId = 003;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = 
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build())
Now I am going to put the code which is used in to display different type notification as shown in Pics above.  Please follow the inline comment properly for better understanding of code. You can Download Code from Github also. After Downloading looks on the NotificationExampleActivity.java class it contain the actual code. 
NotificationExampleActivity.java
package com.example.helloworld;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class NotificationExampleActivity extends Activity implements OnClickListener {
       TextView basic_example_tv,bigtext_example_tv,bigpic_example_tv,inbox_example_tv;
       public final static int NOTI_MODE_BASIC = 0;
       public final static int NOTI_MODE_BIG_TEXT = 1;
       public final static int NOTI_MODE_BIG_PIC = 2;
       public final static int NOTI_MODE_INBOX = 3;
        @Override
       protected void onCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
              setContentView(R.layout.notification_example_activity);

              basic_example_tv = (TextView)findViewById(R.id.basic_example_tv);
              bigtext_example_tv = (TextView)findViewById(R.id.bigtext_example_tv);
              bigpic_example_tv = (TextView)findViewById(R.id.bigpic_example_tv);
              inbox_example_tv = (TextView)findViewById(R.id.inbox_example_tv);

              basic_example_tv.setOnClickListener(this);
              bigtext_example_tv.setOnClickListener(this);
              bigpic_example_tv.setOnClickListener(this);
              inbox_example_tv.setOnClickListener(this);
        }
        @Override
       public void onClick(View view) {
              // TODO Auto-generated method stub
              if(view == basic_example_tv){
                     setNotification(NOTI_MODE_BASIC);
              }
              if(view == bigtext_example_tv){
                     setNotification(NOTI_MODE_BIG_TEXT);
              }
              if(view == bigpic_example_tv){
                     setNotification(NOTI_MODE_BIG_PIC);
              }
              if(view == inbox_example_tv){
                     setNotification(NOTI_MODE_INBOX);
              }
       }

       private void setNotification(int mode){
              int mId = 1;
              String contentTitle = "Notification Test";
              String contentText = "Service Content for basic notification";
              String contentInfo = "Service Info";
              String contentText2 = "Service Content2";
              // Using NotificationCompat to create the notification builder can use
              // the new features introduced after API level 4 without compatibility 
              // problem with lower API level
              NotificationCompat.Builder mBuilder =
                           new NotificationCompat.Builder(this)
              .setSmallIcon(R.drawable.ic_launcher)
              .setContentTitle(contentTitle)
              .setContentText(contentText)
              .setContentInfo(contentInfo);

              // A notification's big view appears only when the notification is expanded,
              // which happens when the notification is at the top of the notification
              // drawer, or when the user expands the notification with a gesture.
              // Expanded notifications are available starting with Android 4.1.
              switch (mode){
              case NOTI_MODE_BIG_TEXT:
                     NotificationCompat.BigTextStyle bigTextStyle
                     = new NotificationCompat.BigTextStyle();
                     bigTextStyle.bigText("Android is a Linux-based operating system designed primarily for touchscreen mobile devices such as smartphones and tablet computers.\nInitially developed by Android, Inc., which Google backed financially and later bought in 2005,[12] Android was unveiled in 2007 along with the founding of the Open Handset Alliance:\n a consortium of hardware, software, and telecommunication companies devoted to advancing open standards for mobile devices.[13] \nThe first Android-powered phone was sold in October 2008");
                     // summary is displayed replacing the position of contentText
                     // if summary is not set, contentInfo will not be displayed too
                     bigTextStyle.setSummaryText(contentText2);                   
                     // Moves the big view style object into the notification object.
                     mBuilder.setStyle(bigTextStyle);
                     break;
              case NOTI_MODE_BIG_PIC:
                     NotificationCompat.BigPictureStyle bigPicStyle
                     = new NotificationCompat.BigPictureStyle();
                     Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.interact);
                     bigPicStyle.bigPicture(bm);
                     bigPicStyle.setSummaryText(contentText2);             
                     // Moves the big view style object into the notification object.
                     mBuilder.setStyle(bigPicStyle);
                     break;
              case NOTI_MODE_INBOX:
                     NotificationCompat.InboxStyle inboxStyle
                     = new NotificationCompat.InboxStyle();
                     String[] events = new String[4];
                     events[0] = "Inbox Line 1";
                     events[1] = "Inbox Line 2";
                     events[2] = "Inbox Line 3";
                     events[3] = "Inbox Line 4";
                     // Moves events into the big view
                     for (int i=0; i < events.length; i++) {
                           inboxStyle.addLine(events[i]);
                     }            
                     inboxStyle.setSummaryText(contentText2);
                     // Moves the big view style object into the notification object.
                     mBuilder.setStyle(inboxStyle);          
                     break;
              case NOTI_MODE_BASIC:
                     break;
              }
              // Creates an explicit intent for an Activity in your app
              Intent resultIntent = new Intent(this, MainActivity.class);

              // The stack builder object will contain an artificial back stack
              // for the started Activity
              // This ensures that navigating backward from the Activity leads out of
              // your application to the Home screen.
              TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
              // Adds the back stack for the Intent (but not the Intent itself)
              stackBuilder.addParentStack(MainActivity.class);
              // Adds the Intent that starts the Activity to the top of the stack
              stackBuilder.addNextIntent(resultIntent);
              // A PendingIntent is used to specify the action which should be performed
              // once the user select the notification.
              PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
              mBuilder.setContentIntent(resultPendingIntent);
              // Just add a fake action here, max 3
              /*    mBuilder.addAction(R.drawable.ic_action_search, "Search", resultPendingIntent);
       mBuilder.addAction(R.drawable.ic_action_search, "Add", resultPendingIntent);
       mBuilder.addAction(R.drawable.ic_action_search, "Save", resultPendingIntent);*/
              // Hide the notification after its selected
              mBuilder.setAutoCancel(true);

              NotificationManager mNotificationManager =
                           (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
              // mId allows you to update the notification later on.
              mNotificationManager.notify(mId, mBuilder.build());
       }

}
following is the example of above notification code. first one is BigPics, 2nd one is big text, 3rd one is basic and 4th one is inbox style notification. 
Now the xml part of above code. notification_example_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     <TextView
        android:id="@+id/notific_examp_tv"
        style="@style/TextViews"
        android:layout_gravity="center_horizontal"
        android:text="@string/notification_world" /> 
    <TextView
        android:id="@+id/basic_example_tv"
        style="@style/TextViews"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="24dp"
        android:background="@drawable/background_shape"
        android:text="@string/notification_basic_example"
        android:textColor="#fff" />
    <TextView
        android:id="@+id/bigtext_example_tv"
        style="@style/TextViews"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="24dp"
        android:background="@drawable/background_shape"
        android:text="@string/notification_bigtext_example"
        android:textColor="#fff" /> 
    <TextView
        android:id="@+id/bigpic_example_tv"
        style="@style/TextViews"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="24dp"
        android:background="@drawable/background_shape"
        android:text="@string/notification_bigpic_example"
        android:textColor="#fff" /> 
    <TextView
        android:id="@+id/inbox_example_tv"
        style="@style/TextViews"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="24dp"
        android:background="@drawable/background_shape"
        android:text="@string/notification_inbox_example"
        android:textColor="#fff" /> 
</LinearLayout>
For more detail you can visit this developer site
you can download this code from Github from following link
Happy Coding !!!

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...