Android[初級教程]第十二章 Notification的應用 – Android移動開發技術文章_手機開發 Android移動開發教學課程

這一章節,我們來學習Notification的應用,很多人問Notification是什麼東東啊?我打個比方吧,還是以西遊記來說:唐僧被妖怪們抓住瞭,那悟空得知道是哪個妖怪抓住瞭他師傅,他得變成一些動物(蒼蠅或蚊子)去通知他師傅啊,通知唐僧悟空來救他瞭,這裡通知就是Notification,那唐僧知道瞭,他就放心瞭,安心等著悟空來救.呵呵,讓我看一下main.xml

 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <Button android:text="通知師傅悟空來救他瞭" android:id="@+id/wukong" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <Button android:text="通知師傅八戒來救他瞭" android:id="@+id/bajie" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <Button android:text="通知師傅沙僧來救他瞭" android:id="@+id/shaseng" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <Button android:text="取消通知" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:id="@+id/notify_cancal"/> 
</LinearLayout> 

這裡面沒有什麼特別的,隻是定義瞭四個按鈕,然後主Activity.java

 
import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
 
public class NotificationDemo extends Activity 

    private final static int NOTIFYCATION_ID = 0x11; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
        // TODO Auto-generated method stub www.aiwalls.com
        super.onCreate(savedInstanceState); 
 
        setContentView(R.layout.notification); 
         
        //查找main.xml中的Button控件 
        Button wukong = (Button) findViewById(R.id.wukong); 
        wukong.setOnClickListener(new ClickEvent()); 
 
        Button bajie = (Button) findViewById(R.id.bajie); 
        bajie.setOnClickListener(new ClickEvent()); 
 
        Button shaseng = (Button) findViewById(R.id.shaseng); 
        shaseng.setOnClickListener(new ClickEvent()); 
         
        Button notify_cancal = (Button)findViewById(R.id.notify_cancal); 
        notify_cancal.setOnClickListener(new ClickEvent()); 
    } 
 
    class ClickEvent implements View.OnClickListener 
    { 
 
        @Override 
        public void onClick(View v) 
        { 
            //獲取系統的NotificationManager服務 
            NotificationManager notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
             
            //創建一個啟動其他Activity的Intent 
            Intent intent = new Intent(NotificationDemo.this, 
                    otherActivity.class); 
            //設置點擊通知時顯示內容啟動的類 
            PendingIntent pi = PendingIntent.getActivity(NotificationDemo.this, 
                    0, intent, 0); 
             
            //創建Notification對象 
            Notification notify = new Notification(); 
             
            //設置Notification的發送時間 
            notify.when = System.currentTimeMillis(); 
            //為Notification設置默認的聲音,默認的振動,默認的閃光燈,但這些需要在AndroidManifest.xml中加入相應的權限,不然會報錯 
            notify.defaults = Notification.DEFAULT_ALL; 
             
            switch (v.getId()) 
            { 
            case R.id.wukong: 
                //設置Notification的圖標 
                notify.icon = R.drawable.wukong; 
                //設置Notification事件信息 
                notify.setLatestEventInfo(NotificationDemo.this, "悟空", 
                        "悟空來救師傅的通知", pi); 
                //設置Notification的文本內容,會顯示在狀態欄中 
                notify.tickerText = "悟空來救師傅瞭"; 
                //發送通知 
                notifyManager.notify(NOTIFYCATION_ID, notify); 
 
                break; 
 
            case R.id.bajie: 
                 
                //同上 
                notify.icon = R.drawable.bajie; 
                notify.setLatestEventInfo(NotificationDemo.this, "八戒", 
                        "八戒來救師傅的通知", pi); 
                notify.tickerText = "八戒來救師傅瞭"; 
                notifyManager.notify(NOTIFYCATION_ID, notify); 
 
                break; 
 
            case R.id.shaseng: 
                 
                //同上 
                notify.icon = R.drawable.shaseng; 
                notify.setLatestEventInfo(NotificationDemo.this, "沙僧", 
                        "沙僧來救師傅的通知", pi); 
                notify.tickerText = "沙僧來救師傅瞭"; 
                notifyManager.notify(NOTIFYCATION_ID, notify); 
 
                break; 
                 
            case R.id.notify_cancal: 
                 
                //取消通知 
                notifyManager.cancel(NOTIFYCATION_ID); 
 
            }    
             
        } 
 
    } 

OK,我們在AndroidManifest.xml中加入相應的權限和要啟動的Activity

 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.kang.button_demo" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="10" /> 
    <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission> 
    <uses-permission android:name="android.permission.VIBRATE"></uses-permission> 
 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <activity android:label="@string/app_name" android:name=".NotificationDemo"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
         
        <activity android:name=".otherActivity"></activity> 
 
    </application> 
</manifest> 
好瞭,都寫完瞭,運行一下呢,如圖所示:

 

 

 

這裡的要點就是Notification和NotificationManager的創建,其實掌握Notification對於Android應用開發還是很有用的,好瞭,這一章就結束瞭,謝謝

摘自:kangkangz4的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *