1. 程式人生 > >Android的狀態列通知(Notification)

Android的狀態列通知(Notification)

通知用於在狀態列顯示訊息,訊息到來時以圖示方式表示,如下:

如果需要檢視訊息,可以拖動狀態列到螢幕下方即可檢視訊息.

1、Layout佈局檔案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="46dp"
        android:onClick="test1"
        android:text="@string/text_notifi" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="94dp"
        android:onClick="clearNoti"
        android:text="@string/text_clear" />

</RelativeLayout>


2、string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">lession16-notifi</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    
    <string name="text_notifi">Notification應用案例</string>
    <string name="text_clear">清除通知</string>

</resources>


3、MainActivity

package com.example.lession16_notifi;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
	private NotificationManager notificationManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void test1(View v){
		showNotification("來簡訊了", "5557", "I love you", R.drawable.ic_launcher, R.drawable.ic_launcher);
	}
	
	public void showNotification(String tickerText,String contentTitle,String contentText,int iconId,int notiId){
		//建立一個Notification
		Notification notification=new Notification();
		//設定通知  訊息  圖示
		notification.icon=iconId;
		//設定發出訊息的內容
		notification.tickerText=tickerText;
		//設定發出通知的時間
		notification.when=System.currentTimeMillis();
		
		//設定顯示通知時的預設的發聲、震動、Light效果
		notification.defaults=Notification.DEFAULT_VIBRATE;//震動
		//Notification notification = new Notification(R.drawable.ic_launcher, "有新的訊息", System.currentTimeMillis());
		
		//3步:PendingIntent android系統負責維護
		PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, getIntent(), 0);
		//4步: 設定更加詳細的資訊
		notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent);
		//5步:使用notificationManager物件的notify方法 顯示Notification訊息   需要制定 Notification的標識
		notificationManager.notify(notiId, notification);
	}
	
	public void clearNoti(View v){
		notificationManager.cancelAll();//清除所有
	}

}


注:模擬器要實現震動需要加許可權:DEFAULT_VIBRATE