Android中Notification的用法

      在消息通知的時候,我們經常用到兩個控件Notification和Toast。特別是重要的和需要長時間顯示的信息,用Notification最合適不過了。他可以在頂部顯示一個圖標以標示有了新的通知,當我們拉下通知欄的時候,可以看到詳細的通知內容。
      最典型的應用就是未看短信和未接來電的顯示,還有QQ微信,我們一看就知道有一個未接來電或者未看短信,收到QQ離線信息。同樣,我們也可以自定義一個Notification來定義我們自己的程序想要傳達的信息。

Notification我把他分爲兩種,一種是默認的顯示方式,另一種是自定義的。

【一】默認的顯示方式:

[1]程序框架結構圖如下


[2]佈局文件 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="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView    
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:gravity="center" 
    android:textColor="#EEE" 
    android:textStyle="bold" 
    android:textSize="25sp" 
    android:text="NotificationDemo實例" /> 
<Button 
    android:id="@+id/btnSend" 
    android:text="send notification" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center"/>      
</LinearLayout>
[3] MainActivity.java源碼如下:

package com.andyidea.notification;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
	private Button btnSend;  
    
    //定義BroadcastReceiver的action  
    private static final String NotificationDemo_Action = "com.andyidea.notification.NotificationDemo_Action";  
      
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        
        btnSend = (Button)findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                Intent intent = new Intent();  
                intent.setAction(NotificationDemo_Action);  
                sendBroadcast(intent);  
            }  
        });  
    }  
      
}
[4] 佈局文件 secondlayou.xml 源碼如下:

<?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">
  <TextView    
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:gravity="center" 
    android:textColor="#EEE" 
    android:textStyle="bold" 
    android:textSize="25sp" 
    android:text="顯示通知界面" /> 
<Button 
    android:id="@+id/btnCancel" 
    android:text="cancel notification" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" />      
</LinearLayout>
[5] SecondActivity.java源碼如下:

package com.andyidea.notification;

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 SecondActivity extends Activity {

	private Button btnCancel;  
    //聲明Notification  
    private Notification notification;  
    //聲明NotificationManager  
    private NotificationManager mNotification;  
    //標識Notification的ID  
    private static final int ID = 1;  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.secondlayout);  
          
        btnCancel = (Button)findViewById(R.id.btnCancel); 
        //怎樣獲得NotificationManager的實例?  
        String service = NOTIFICATION_SERVICE;  
        mNotification = (NotificationManager)getSystemService(service);  
          
        //獲得Notification的實例  
        notification = new Notification();  
          
        //設置該圖標 會在狀態欄顯示  
        int icon = notification.icon = android.R.drawable.stat_sys_phone_call;  
        //設置提示信息  
        String tickerText = "Test Notification";  
        //設置顯示時間  
        long when = System.currentTimeMillis();  
        notification.icon = icon;  
        notification.tickerText = tickerText;  
        notification.when = when;  
          
        Intent intent = new Intent(this, MainActivity.class);  
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);  
        notification.setLatestEventInfo(this, "消息", "SMS Android", pi);  
        mNotification.notify(ID, notification);  
          
        btnCancel.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                mNotification.cancel(ID);  //--->取消通知  
            }  
        });  
    }  
      
}
[6] NotificationReceiver.java源碼如下:

package com.andyidea.notification;

import com.andyidea.notification.SecondActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class NotificationReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		//實例化Intent  
        Intent i = new Intent();  
        //在新任務中啓動Activity  
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        //設置Intent啓動的組件名稱  
        i.setClass(context, SecondActivity.class);  
        //啓動Activity,顯示通知  
        context.startActivity(i);  
	}

}
[7]程序運行效果如下:






【二】自定義顯示方式:

未完待續...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章