android notification通知的…

来源:互联网 发布:jdk 7u55 linux x64 编辑:程序博客网 时间:2024/05/18 00:01

android <wbr>notification通知的简单使用

1.内容的标题
2.大图标
3.内容文本
4.内容数量
5.小图标
6.通知时间

//创建一个简单的通知
public void showTimeNotification(View view){
 
    NotificationCompat.BuildermBuilder =
    newNotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("Mynotification")
    .setContentText("HelloWorld!")
    .setNumber(10);
  mBuilder.setAutoCancel(true);//设置当用户点击通知后,通知消失
 
  //创建一个意图,点击通知后会跳转到意图指定的activity里
  Intent resultIntent = newIntent(this,AActivity.class);
  
  TaskStackBuilder stackBuilder =TaskStackBuilder.create(this);
  stackBuilder.addNextIntent(resultIntent);
  PendingIntent resPendingIntent=
    stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
  
  mBuilder.setContentIntent(resPendingIntent);
  NotificationManagermNotificationManager =
    (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  mNotificationManager.notify(1,mBuilder.build());
 
 }

//创建一个下载进度条的通知

public void showTimeNotification(View view){

 final NotificationManager mNotifyManager=
         (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  finalNotificationCompat.Builder mBuilder = newNotificationCompat.Builder(this);
  mBuilder.setContentTitle("PictureDownload")
     .setContentText("Download in progress")
     .setSmallIcon(R.drawable.ic_launcher);
  // Start a lengthy operation ina background thread
  new Thread(
     new Runnable() {
         @Override
         public void run() {
             int incr;
             // Do the "lengthy" operation 20 times
             for (incr = 0; incr <= 100; incr+=5) {
                     // Sets the progress indicator to a max value, the
                     // current completion percentage, and "determinate"
                     // state
                     mBuilder.setProgress(100, incr, false);
                     // Displays the progress bar for the first time.
                     mNotifyManager.notify(0, mBuilder.build());
                         // Sleeps the thread, simulating an operation
                         // that takes time
                         try {
                             // Sleep for 5 seconds
                             Thread.sleep(1000);
                         } catch (InterruptedException e) {
                             Log.d("TAG", "sleep failure");
                         }
             }
             // When the loop is finished, updates the notification
          mNotifyManager.cancel(0);//移除
             mBuilder.setContentText("Download complete")
                     .setProgress(0,0,false);
             mNotifyManager.notify(1, mBuilder.build());
         }
     }
  // Starts the thread by callingthe run() method in its Runnable
  ).start();

}

0 0
原创粉丝点击