2013年5月28日 星期二

Notification with Big View

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.

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_basic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Basic" />

    <Button
        android:id="@+id/btn_bigtext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Big Text" />

    <Button
        android:id="@+id/btn_bigpic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Big Pic" />

    <Button
        android:id="@+id/btn_inbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Inbox" />

</LinearLayout>
Simply 4 buttons are added here to demonstrate different styles of notification.

MainActivity.java
package com.example.sample_service_notification;

import android.os.Bundle;
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.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    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;
	
    private Button btn_basic;
    private Button btn_bigtext;
    private Button btn_bigpic;
    private Button btn_inbox;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        findViews();
        setListieners();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    private void findViews(){
    	btn_basic = (Button) this.findViewById(R.id.btn_basic);
    	btn_bigtext = (Button) this.findViewById(R.id.btn_bigtext);
    	btn_bigpic = (Button) this.findViewById(R.id.btn_bigpic);
    	btn_inbox = (Button) this.findViewById(R.id.btn_inbox);
    }
    
    private void setListieners(){
    	btn_basic.setOnClickListener(btn_basic_click);
    	btn_bigtext.setOnClickListener(btn_bigtext_click);
    	btn_bigpic.setOnClickListener(btn_bigpic_click);
    	btn_inbox.setOnClickListener(btn_inbox_click);
    }
  
    private OnClickListener btn_basic_click = new OnClickListener() {
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		setNotification(NOTI_MODE_BASIC);
    	}
    };
	
    private OnClickListener btn_bigtext_click = new OnClickListener() {
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		setNotification(NOTI_MODE_BIG_TEXT);
    	}
    };
	
    private OnClickListener btn_bigpic_click = new OnClickListener() {
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		setNotification(NOTI_MODE_BIG_PIC);
    	}
    };
	
    private OnClickListener btn_inbox_click = new OnClickListener() {
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		setNotification(NOTI_MODE_INBOX);		
    	}
    };
    
    private void setNotification(int mode){
    	int mId = 1;
    	String contentTitle = "Service Notification Test:";
    	String contentText = "Service Content";
    	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("Big Text Line 1\nBig Text Line 2\nBig Text Line 3");
    		// 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());
    }
    
}
Focus on the function setNotification, everything is done there. Some fake builder actions were added here, it will be further elaborated later on.

Result:

After clicking the "Basic" button, a notification icon appears in the notification bar

Drag down the notification bar, here is how the "Basic" notification look like

Big View Notification with style of "Big Text"

Big View Notification with style of "Big Picture"

Big View Notification with style of "Inbox"

沒有留言:

張貼留言