Notification新舊用法

  
    //顯示通知信息  

    //api 11 版本之前:
      
    protected void showNotification() {  
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
        //Notification notification = new Notification(this,R.drawable.ic_launcher);  
        Notification notification = new Notification(R.drawable.ic_launcher,"",System.currentTimeMillis() );  
        PendingIntent contentIndent = PendingIntent.getActivity(MainActivity.this, 0, 
        new Intent(MainActivity.this,MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);  
        notification.setLatestEventInfo(MainActivity.this, "您的BMI值過高", "通知監督人", contentIndent);  
        //加i是爲了顯示多條Notification  
        notificationManager.notify(i,notification);  
        i++;  
    } 


     // api 11 版本之後:

     protected void showNotification2() {  
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
        Builder builder = new Notification.Builder(MainActivity.this);  
        PendingIntent contentIndent = PendingIntent.getActivity(MainActivity.this, 0, 
        new Intent(MainActivity.this,MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);  
        builder.setContentIntent(contentIndent).setSmallIcon(R.drawable.ic_launcher)//設置狀態欄裏面的圖標(小圖標)                     .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.i5))//下拉下拉列表裏面的圖標(大圖標)        .setTicker("this is bitch!") //設置狀態欄的顯示的信息  
               .setWhen(System.currentTimeMillis())//設置時間發生時間  
               .setAutoCancel(true)//設置可以清除  
               .setContentTitle("This is ContentTitle")//設置下拉列表裏的標題  
               .setContentText("this is ContentText");//設置上下文內容  
        Notification notification = builder.getNotification();  
        //加i是爲了顯示多條Notification  
        notificationManager.notify(i,notification);  
        i++;
    }  

 


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