5.Notification通知

這個稱之爲通知,顯示在手機的通知欄,用戶能夠清除,能夠點擊java

實現的代碼以下:android

package lovefang.stadyService;
 import android.content.Intent;
 import android.os.Bundle;
 import android.app.Activity;
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.net.Uri;
 import android.media.RingtoneManager;
 import android.widget.Button;
 import android.view.View;
/**使用notification*/
 public class UseNotification extends Activity {
   /**建立組件*/
  private Button textButton;
  private Button soundButton;// 聲音通知
  private Button vibrateButton;// 震動通知
  private Button ledButton;// led通知
  private Button offButton;// 關閉通知
  NotificationManager notificationManager;
   /**建立Activity*/
  public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.notification);
   getComment();
   registerComment();
  }
   /**獲取對象*/
  public void getComment(){
    /**獲取Notification對象*/
   notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
   textButton = (Button)findViewById(R.id.notificationMessage);
   soundButton =(Button)findViewById(R.id.notificationSound);
   vibrateButton = (Button)findViewById(R.id.notificationVibrate);
   ledButton = (Button)findViewById(R.id.notificationLED);
   offButton = (Button)findViewById(R.id.offnotification);
  }
   /**註冊對象*/
  public void registerComment(){
   textButton.setOnClickListener(notificationMessage);
   soundButton.setOnClickListener(notificationSound);
   vibrateButton.setOnClickListener(notificationVibrate);
   ledButton.setOnClickListener(notificationLed);
   offButton.setOnClickListener(notificationOff);
  }
  public Button.OnClickListener notificationMessage = new Button.OnClickListener(){
   public void onClick(View view){
    Notification notification = new Notification();// 建立Notification對象
    notification.icon = R.drawable.icon;
    notification.tickerText = "This is text notication";// 設置通知消息
     /**單擊通知後的Intent,此例子單擊後仍是在當前頁面*/
    PendingIntent intent = PendingIntent
     .getActivity(UseNotification.this,
       0, new Intent(UseNotification.this,UseNotification.class)
       , 0);
     /**設置通知消息*/
    notification.setLatestEventInfo(UseNotification.this
      ,"Notification","Content of Notification Demo",intent);
     /**執行通知*/
    notificationManager.notify(0, notification);
   }
  };
  public Button.OnClickListener notificationSound = new Button.OnClickListener(){
   public void onClick(View view){
     /**建立通知對象*/
    Notification notification = new Notification();
     /**獲取系統當前聲音*/
    String ringName = RingtoneManager.getActualDefaultRingtoneUri(
      UseNotification.this, RingtoneManager.TYPE_RINGTONE)
      .toString();
     /**設置系統當前鈴聲爲此通知的鈴聲*/
    notification.sound = Uri.parse(ringName);
     /**執行通知*/
    notificationManager.notify(0,notification);
   }
  };
   /**震動通知*/
  public Button.OnClickListener notificationVibrate = new Button.OnClickListener(){
   public void onClick(View view){
    Notification notification = new Notification();// 建立Notification對象
    notification.vibrate = new long[] {0, 100, 200, 300};// 設置通知震動模式
    notificationManager.notify(0,notification);// 執行通知
   }
  };
   /**LED通知*/
  public Button.OnClickListener notificationLed = new Button.OnClickListener(){
   public void onClick(View view){
    Notification notification = new Notification();// 建立Notification對象
    notification.ledOnMS = 300;// 設置led開始閃光的時間
    notification.ledOffMS = 1000;// 設置關閉時的閃光時間
    notificationManager.notify(0,notification);// 執行通知
   }
  };
   /**關閉通知*/
  public Button.OnClickListener notificationOff = new Button.OnClickListener(){
   public void onClick(View view){
    notificationManager.cancel(0);// 關閉通知
   }
  };
 }
相關文章
相關標籤/搜索