Android NotificationManager简单使用详解

本文实例为大家分享了Android NotificationManager的简单使用代码,供大家参考,具体内容如下

我们有时候需要使用通知,先要获得一个通知管理器,然后通过通知管理器来发送通知。以下就是几种通知的使用:

public class MainActivity extends Activity {
 
 private NotificationManager manager;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  //1.使用通知,先要获得一个通知管理器
  manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  //点击此按钮发出一条通知(普通的通知)
  findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View v) {
    Builder builder = new NotificationCompat.Builder(MainActivity.this);    
    builder.setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("我是测试用的普通通知").setContentText("测试测试");
    //当通知被点击的时候使用
    Intent intent = new Intent();
    intent.setClass(MainActivity.this, MainActivity.class);
    //PendingIntent 等待的意图
    PendingIntent intent2 = PendingIntent.getActivity(MainActivity.this, 0, intent, Notification.FLAG_AUTO_CANCEL);
 
    builder.setContentIntent(intent2);
    //通知点击以后消失
    builder.setAutoCancel(true);
    Notification notification = builder.build();
    //发送通知
    manager.notify(0,notification);
   }
  });
 
  findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
            //进度通知
   @Override
   public void onClick(View v) {
    new Thread(){
     public void run() {
      Builder builder = new NotificationCompat.Builder(MainActivity.this);    
      for (int i = 0; i <=100; i++) {
       //progress当前进度
       //indeterminate是否精确
       builder.setProgress(100,i , false);
       manager.notify(1,builder.build());
                            //必须设置setSmallIcon
       builder.setSmallIcon(R.drawable.ic_launcher);
       try {
        Thread.sleep(200);
       } catch (InterruptedException e) {
        e.printStackTrace();
       }
      }
      manager.cancel(1);  
     };
    }.start();
   }
  });
  
  
  findViewById(R.id.button3).setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
            Builder builder = new NotificationCompat.Builder(MainActivity.this);   
   builder.setSmallIcon(R.drawable.ic_launcher)
   .setContentTitle("标题")
   .setContentText("test");
   BigPictureStyle style = new NotificationCompat.BigPictureStyle();
      style.bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.c));
      style.setBigContentTitle("通知");
      builder.setStyle(style);
      manager.notify(2, builder.build());
   }
  });
  
  findViewById(R.id.button4).setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
             Builder builder = new NotificationCompat.Builder(MainActivity.this);    
    builder.setSmallIcon(R.drawable.ic_launcher);
             RemoteViews views=new RemoteViews(getPackageName(),R.layout.notify_item);
             views.setImageViewResource(R.id.iv, R.drawable.ic_launcher);
             views.setTextViewText(R.id.tv,"自定义视图");
             builder.setContent(views);
             manager.notify(3,builder.build());
   }
  });
 }
 
}

Android NotificationManager简单使用详解_第1张图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android NotificationManager简单使用详解)