Notification的使用

本文深入讲解了Android系统中通知机制的实现方式,包括LV16以前的直接创建Notification方法和LV16以后采用Notification.Builder构建通知的方式。通过具体代码示例,展示了如何设置通知的各种属性,如图标、标题、内容、跳转Intent以及使用NotificationManager显示和清除通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、在LV16以前的用法

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);
  }

  
  
  public void test1(View v){
    //Toast.makeText(this, "点击我了", Toast.LENGTH_LONG).show();
    showNotification("来短信了", "5554", "I love you", R.drawable.ic_launcher, R.drawable.ic_launcher);
    
  }
  
  
  public void showNotification(String tickerText,String contentTitle,String contentText,int iconId,int notiId){
    //2步创建一个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.cancel(notiId);//清除具体的Notifaction
    notificationManager.cancelAll();//清除所有
  }

}
View Code

二、在LV16以后的用法

//设置Intent跳转
Intent intent = new Intent(this,OtherActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        //利用Notification.Builder创建Notification
        Notification.Builder notification = new Notification.Builder(this);
        notification.setAutoCancel(true);
        notification.setSmallIcon(R.mipmap.ic_launcher);
        notification.setContentTitle("Hello World");
        notification.setContentText("I am a ET");
        notification.setContentIntent(pendingIntent);
        //创建Notification
        Notification notification1 = notification.build();
        //获取Notification管理器
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //执行
        manager.notify(0,notification1)
View Code

 

转载于:https://www.cnblogs.com/rookiechen/p/5427230.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值