5.Notification通知

这个称之为通知,显示在手机的通知栏,用户可以清除,可以点击

实现的代码如下:

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);// 关闭通知
   }
  };
 }

转载于:https://my.oschina.net/u/1014520/blog/194040

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值