1. 程式人生 > >Android基礎_通知(Notification)

Android基礎_通知(Notification)

通知(Notification)是Android系統中比較有特色的一個功能,當某個應用程式希望向使用者發出一些提示資訊,而該應用程式又不在前臺執行時,就可以藉助通知來實現。發出一條通知後,手機最上方的狀態列中會顯示一個通知的圖示,下拉狀態列後可以看到通知的詳細內容。通知可以在活動、廣播、服務中建立。

一、通知建立的過程

1.我們需要NotificationManager來對通知進行管理,一般用getSystemService()方法來獲取其物件。

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

2.然後就是建立Notification的物件。

Notification notification = new Notification(R.drawable.icon, "This is ticker text",System.currentTimeMillis());

引數一:通知欄的圖示

引數二:指定通知的提示內容

引數三:設定通知被建立的時間

3.設定通知的佈局

標準佈局setLatestEventInfo();

notification.setLatestEventInfo(context, "This is content title", "This is content text", null);

引數一:context

引數二:通知的標題

引數三:通知的具體內容

引數四:點選通知的響應,為null表示無響應

以後會學到自定義佈局

4.顯示通知

manager.notify(1,notification);

引數一:通知的id,方便NotificationManager對通知進行管理

引數二:建立的通知

二、設定通知點選後的響應(PendingIntent)

Intent intent = new Intent(this, NotificationActivity.class);

PendingIntent pi = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);

notification.setLatestEventInfo(this, "This is content title", "This is content text", pi);

三、一個簡單的例子

MainActivity.java

package com.myNotification;

import java.io.File;
import com.example.mytest_notification.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
	
	Button open, close;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		initObjects();
	}
	
	private void initObjects(){
		open = (Button) findViewById(R.id.xml_open);
		close = (Button) findViewById(R.id.xml_close);
		open.setOnClickListener(this);
		close.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.xml_open:
				openNotification();
				break;
			case R.id.xml_close:
				closeNotification();
				break;
			default:
				break;
		}
	}
	
	@SuppressLint("SdCardPath") @SuppressWarnings("deprecation")
	private void openNotification(){
		NotificationManager manage = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.ic_launcher, "戚東亮看看", System.currentTimeMillis());
		Intent intent = new Intent(this, NotificationActivity.class);
		PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		notification.setLatestEventInfo(this, "標題", "具體內容", pi);
		Uri soundUri = Uri.fromFile(new File("/sdcard/Music/You Are Beautiful.mp3"));
		notification.sound = soundUri;
		long[] vibrates = {0, 500, 500, 500};
		notification.vibrate = vibrates;
		manage.notify(1, notification);
	}
	
	private void closeNotification(){
		NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		manager.cancelAll();
	}

}


NotificationActivity.java

package com.myNotification;

import com.example.mytest_notification.R;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

public class NotificationActivity extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification_layout);
		NotificationManager manage = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		manage.cancel(1);
	}

}