• Notification的使用


    一、在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
  • 相关阅读:
    召开演示会议和总结会议
    召开每天的站立会议
    禅道管理中的项目管理--组织进行任务分解
    linux sort,uniq,cut,wc命令详解
    json2.js的用途(拯救IE)
    memcache的内存回收机制
    memcache内存分配机制
    Linux之Sed命令详解(总结一些实用例子)
    CentOS 设置网络(修改IP&修改网关&修改DNS)--update.14.08.15
    php中文字符串翻转
  • 原文地址:https://www.cnblogs.com/rookiechen/p/5427230.html
Copyright © 2020-2023  润新知