android開發之Notification學習筆記

今天總結了一下Notification的使用,與你們分享一下。
MainActivity.java:html

本文參考:http://www.jb51.net/article/36567.htmhttp://www.cnblogs.com/linjiqin/archive/2011/12/14/2288074.htmljava

public class MainActivity extends Activity {

    private Button btn;
    private NotificationManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// showNotification();
// showNotification2();
// showNotification3();
// showNotification4();
// showNotification5();
        showNotification6();
        this.btn = (Button) this.findViewById(R.id.button1);
        this.btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                clearNotification();
            }
        });
    }
    /** * 自定義Notification佈局 */
    private void showNotification6() {
        RemoteViews remoteViews = new RemoteViews(getPackageName(),
                R.layout.custom_notification);
        Intent intent = new Intent(this, SecondActivity.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                intent, 0);
        //點擊時給SecondActivity發送一條廣播
        remoteViews.setOnClickPendingIntent(R.id.paly_pause_music,
                pendingIntent);
        Intent intent1 = new Intent(this,SecondActivity.class);
        remoteViews.setOnClickPendingIntent(R.id.paly_pause_music, PendingIntent.getActivity(this, 0, intent1, 0));
        NotificationCompat.Builder builder = new Builder(this);
        builder.setContent(remoteViews).setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)).setOngoing(true)
                .setTicker("music is playing");
        manager.notify(0, builder.build());
    }
    /** * 大布局Picture類型notification * 這個方法貌似還有一點問題 */
    private void showNotification5() {
        NotificationCompat.BigPictureStyle pictureStyle = new BigPictureStyle();
        pictureStyle.setBigContentTitle("BigContentTitle")
        .setSummaryText("Summary Text").bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
        Notification notification = new NotificationCompat.Builder(this)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
        .setTicker("showBigView_pic").setContentInfo("ContentInfo")
        .setContentTitle("ContentTitle").setContentText("ContentText")
        .setStyle(pictureStyle)
        .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
        .build();
        manager.notify(3, notification);
    }
    /** * 帶進度條的通知欄 */
    private void showNotification4() {
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
        .setSmallIcon(R.drawable.ic_launcher)
        .setTicker("showProgressBar")
        .setContentInfo("ContentInfo")
        .setOngoing(true)
        .setContentTitle("ContentTitle")
        .setContentText("ContentText");
        new Thread(new Runnable() {

            @Override
            public void run() {
                int progress = 0;
                for(progress=0;progress<100;progress+=5){
                    //將第三個參數改成true,進度條會變爲無明顯進度的樣式
                    builder.setProgress(100, progress, true);
                    manager.notify(0, builder.build());
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                    }
                }
                //線程執行完畢以後,修改通知欄的顯示
                builder.setContentTitle("Download complete").setProgress(0, 0, false).setOngoing(false);
                manager.notify(0, builder.build());
            }
        }).start();

    }
    /** * 大通知欄 */
    private void showNotification3() {
        NotificationCompat.BigTextStyle textStyle = new BigTextStyle();
        textStyle.setBigContentTitle("BigContentTitle")
        .setSummaryText("Summary Text")
        .bigText("I'm Big Text......................................................................");
        Notification notification = new NotificationCompat.Builder(this)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
        .setSmallIcon(R.drawable.ic_launcher)
        .setTicker("Ticker")
        .setContentInfo("ContentInfo")
        .setContentTitle("ContentTitle")
        .setContentText("ContentText")
        .setStyle(textStyle)
        .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).build();
        manager.notify(0, notification);
    }
    /** * 小通知欄 */
    private void showNotification2() {
        Notification notification = new NotificationCompat.Builder(this)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
        .setSmallIcon(R.drawable.ic_launcher)
        .setTicker("showNormal")
        .setContentInfo("ContextInfo")
        .setContentTitle("ContentTitle")
        .setContentText("ContentText")
        .setNumber(45)
        .setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .build();
        Intent notificationIntent =new Intent(MainActivity.this, SecondActivity.class);
        PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent=contentItent;
        manager.notify(0, notification);
    }
    /** * 在狀態欄顯示通知 */
    private void showNotification(){
        // 建立一個NotificationManager的引用
        NotificationManager notificationManager = (NotificationManager)    
            this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);   

        // 定義Notification的各類屬性
        Notification notification =new Notification(R.drawable.ic_launcher,   
                "系統測試", System.currentTimeMillis()); 
        //FLAG_AUTO_CANCEL 該通知能被狀態欄的清除按鈕給清除掉
        //FLAG_NO_CLEAR 該通知不能被狀態欄的清除按鈕給清除掉
        //FLAG_ONGOING_EVENT 通知放置在正在運行
        //FLAG_INSISTENT 是否一直進行,好比音樂一直播放,知道用戶響應
// notification.flags |= Notification.FLAG_ONGOING_EVENT; // 將此通知放到通知欄的"Ongoing"即"正在運行"組中 
// notification.flags |= Notification.FLAG_NO_CLEAR; // 代表在點擊了通知欄中的"清除通知"後,此通知不清除,常常與FLAG_ONGOING_EVENT一塊兒使用 
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;   
        //DEFAULT_ALL 使用全部默認值,好比聲音,震動,閃屏等等
        //DEFAULT_LIGHTS 使用默認閃光提示
        //DEFAULT_SOUNDS 使用默認提示聲音
        //DEFAULT_VIBRATE 使用默認手機震動,需加上<uses-permission android:name="android.permission.VIBRATE" />權限
        notification.defaults = Notification.DEFAULT_LIGHTS; 
        //疊加效果常量
        //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
        notification.ledARGB = Color.BLUE;   
        notification.ledOnMS =5000; //閃光時間,毫秒

        // 設置通知的事件消息 
        CharSequence contentTitle ="系統消息"; // 通知欄標題 
        CharSequence contentText ="系統正在運行...."; // 通知欄內容 
        Intent notificationIntent =new Intent(MainActivity.this, SecondActivity.class); // 點擊該通知後要跳轉的Activity 
        PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0);   
        notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);   

        // 把Notification傳遞給NotificationManager 
        notificationManager.notify(0, notification);   
    }
    //刪除通知 
    private void clearNotification(){
        // 啓動後刪除以前咱們定義的通知 
        NotificationManager notificationManager = (NotificationManager) this 
                .getSystemService(NOTIFICATION_SERVICE);   
        notificationManager.cancel(0);  

    }
}

關鍵代碼文中已有註釋,完整代碼下載。若有錯誤以後歡迎你們一塊兒討論。android

版權聲明:本文爲博主原創文章,未經博主容許不得轉載。如有錯誤地方,還望批評指正,不勝感激。markdown

相關文章
相關標籤/搜索