亲宝软件园·资讯

展开

Android NotificationManager简单使用

luffy207 人气:0

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

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

加载全部内容

相关教程
猜你喜欢
用户评论