Android Studio 学习(七)通知

导入support- -v41.进入 file-project structure2.左边选择app3.右边选择dependencies4.左下角可以看到一个加号,点击选择Library dependency就可以看到可以导入的jar包,选择support-v4导入即可简单通知
  • 通知管理工具
    `NotificationManager manager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

  • 通知对象
    Notification notification = new NotificationCompat.Builder(MainActivity.this,"default") .setContentTitle("title") .setContentText("text") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .build();

  • 显示通知
    manager.notify(1,notification);

  • 通知的点击功能
  • Intent intent = new Intent(MainActivity.this,NotificationActivity.class);
  • PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this,0,intent,0);
  • Notification notification = new NotificationCompat.Builder(MainActivity.this,"default")
    .setContentTitle("title")
    .setContentText("text")
    .setWhen(System.currentTimeMillis())
    .setSmallIcon(R.mipmap.ic_launcher)
    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
    .setContentIntent(pendingIntent)//点击功能
    .setDefaults(NotificationCompat.DEFAULT_ALL)
    .setPriority(NotificationCompat.PRIORITY_MAX)
    .build();

    相关内容推荐