通知 之 Notification

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;
public class NotifyActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notify);
		overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
	}
   
	//發通知,如果震動的話要在配置文件中加權限
	 public void clickme(View v){
			//通知的管理者
			NotificationManager manage=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
			
			Intent notificationIntent = new Intent(this,InfoActivity.class);
		    //將來要進行的intent
			PendingIntent contentIntent  = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 
			
			Notification info = new Notification();
			info.icon=R.drawable.ic_launcher;
		    info.setLatestEventInfo(getApplicationContext(), "android","加油,小高...", contentIntent);   
		    info.defaults |=Notification.DEFAULT_SOUND;
		   // info.flags |= info.FLAG_INSISTENT; //重複發出聲音,直到用戶響應此通知
		    info.defaults |=Notification.DEFAULT_LIGHTS;
		    info.defaults |=Notification.DEFAULT_VIBRATE;
	        // 100 毫秒延遲後,震動 250 毫秒,暫停 100 毫秒後,再震動 500 毫秒   
	        info.vibrate = new long[] { 100, 250, 100, 500 };   
		    info.tickerText = "小高,爲未來加油!關於android的學習深度...";
	        info.when = System.currentTimeMillis();
			
	       //把通知交給通知管理器去顯示
			manage.notify(110,info);
			        
	        //準備一個通知建造器,但這種方法行不通,或許是android沒做好,經過測試是行不通,所以就註釋了。
			/*Builder builder=new Notification.Builder(this);
			// 設置相關的通知屬性
			builder.setSmallIcon(R.drawable.ic_launcher); //圖標
			builder.setContentTitle("小高,爲未來加油!"); //通知的標題
			builder.setTicker("小高,關於android的學習深度..."); //通知簡寫段
			builder.setAutoCancel(true);//設置可以清除	
			builder.setContentText("詳細內容請打開查看...."); //通知的內容
			builder.setContentIntent(contentIntent);  //設置將來要去的intent
			//Notification info=builder.build(); */	
	 }
}

xml佈局如下:

<LinearLayout 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=".NotifyActivity"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="這裏是通知哦"  android:gravity="center_horizontal"/>
   <Button android:id="@+id/sendNotify" android:text="發通知" android:onClick="clickme" style="@style/btnStyle" />
    
</LinearLayout>


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