Android學習筆記(一)之Notification

來源:互聯網
上載者:User

首先建立一個Android工程

然後編輯main.xml

代碼如下:

[xhtml] 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<Button   
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="@string/send" 
    android:id="@+id/button" 
    /> 
</LinearLayout> 

再編輯strings.xml

[xhtml] 
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">通知</string> 
    <string name="app_name">狀態列通知</string> 
<string name="send">發送通知</string> 
</resources> 

然後就是對其Notification進行處理

 

下面來看一下建立並顯示一個Notification的步驟。建立和顯 示一個Notification需要如下5步:

1. 通過getSystemService方法獲得一個NotificationManager對象。

2. 建立一個Notification對象。每一個Notification對應一個Notification對象。在這一步需要設定顯示在螢幕上方狀態列的通知訊息、通知訊息前方的映像資源ID和發出通知的時間。一般為目前時間。

3. 由於Notification可以與應用程式脫離。也就是說,即使應用程式被關閉,Notification仍然會顯示在狀態列 中。當應用程式再次啟動後,又可以重新控制這些Notification。如清除或替換它們。因此,需要建立一個PendingIntent對象。該對象由Android系統負責維護,因此,在應用程式關閉後,該對象仍然不會被釋放。

4. 使用Notification類的setLatestEventInfo方法設定Notification的詳細資料。

5. 使用NotificationManager類的notify方法顯示Notification訊息。在這一步需要指定標識Notification的唯一ID。這個ID必須相對於同一個NotificationManager對象是唯一的,否則就會覆蓋相同ID的Notificaiton。

心動不如行動,下面我們來演練一下如何在狀 態欄顯示一個Notification,代碼如下:

 

[java] 
package com.cayden.notification; 
 
import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
/**
 * @author 崔冉
 *
 */ 
public class NotificationActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        Button button=(Button)this.findViewById(R.id.button); 
        button.setOnClickListener(new View.OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                /**
                 * 擷取通知管理器
                 */ 
                NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
                int icon=android.R.drawable.sym_action_email; 
                long when=System.currentTimeMillis(); 
                /**
                 * 建立一個通知指定表徵圖和標題
                 */ 
                Notification notification=new Notification(icon,null,when); 
                notification.defaults=Notification.DEFAULT_SOUND;//發出預設聲音 
                PendingIntent contenIntent=PendingIntent.getActivity(NotificationActivity.this, 0, null, 0); 
                notification.setLatestEventInfo(NotificationActivity.this, "開會通知", "今天下午四點半在512開會!", contenIntent); 
                 
                 
                notificationManager.notify(0, notification);//發送通知 
                 
            } 
        }); 
    } 
} 

 

 運行效果:

 

 

 

點擊發送通知按鈕

後的效果

 

 

 

 

 在左上方多出一個郵件符號.

然後用滑鼠選中往下拖

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.