注意阅读:
NotificationCompat.Builder()过时,失效:
https://blog.csdn.net/qq_32534441/article/details/103501273
简介
- 这是一种具有全局效果的通知
- 最典型的例子就是QQ了
- 一般比较重要的通知,我们都是使用这个来通知用户的
使用
记得要导入包哦!
compile 'com.android.support:support-v4:+'
- 1
实例:
public class MainActivity extends AppCompatActivity
{
private NotificationManager notificationManager;
private NotificationCompat.Builder notificationCompat;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationCompat=new NotificationCompat.Builder(this);
notificationCompat.setContentText("内容");
notificationCompat.setContentTitle("标题");
notificationCompat.setSmallIcon(R.drawable.ic_launcher);
findViewById(R.id.mainButton1).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View p1)
{
notificationManager.notify(1, notificationCompat.build());
}
});
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
效果图:
怎么回事?怎么和QQ的效果不一样呢?别着急,我们继续往下看,在里面再添加这两段代码,然后我们再试试!
notificationCompat.setPriority(NotificationCompat.PRIORITY_MAX);
notificationCompat.setDefaults(NotificationCompat.DEFAULT_ALL);
- 1
- 2
效果图:
嘿嘿,已经可以了,接下来,让我们为它设置一个点击事件吧!
- Notification不仅仅能够对用户进行通知,还能够与用户发生互动
- 我们可以通过PendingIntent为我们的Notification设置点击事件
- PendingIntent可选的主要有getActivity、getBroadcast、getService
添加下面的两段代码
Intent in=new Intent(Intent.ACTION_VIEW,Uri.parse("https://m.baidu.com"));
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,in,0);
- 1
- 2
//为Notification整体设置一个点击事件
notificationCompat.setContentIntent(pendingIntent);
- 1
- 2
完整代码:
public class MainActivity extends AppCompatActivity
{
private NotificationManager notificationManager;
private NotificationCompat.Builder notificationCompat;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("https://m.baidu.com"));
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationCompat=new NotificationCompat.Builder(this);
notificationCompat.setContentText("内容");
notificationCompat.setContentTitle("标题");
notificationCompat.setSmallIcon(R.drawable.ic_launcher);
notificationCompat.setContentIntent(pendingIntent);
findViewById(R.id.mainButton1).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View p1)
{
notificationManager.notify(1, notificationCompat.build());
}
});
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
效果图
通知栏还支持设置一个进度条!
- 进度条分为两种
- 确定进度条和不确定进度条:
实例:
public class MainActivity extends AppCompatActivity
{
private NotificationManager notificationManager;
private NotificationCompat.Builder notificationCompat;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent mIntent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://m.baidu.com"));
PendingIntent mPendingIntent=PendingIntent.getActivity(this, 0, mIntent, 0);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationCompat = new NotificationCompat.Builder(this);
notificationCompat.setContentText("正在下载中");
notificationCompat.setContentTitle("MyAPP");
notificationCompat.setSmallIcon(R.drawable.ic_launcher);
notificationCompat.setDefaults(NotificationCompat.DEFAULT_ALL);
notificationCompat.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationCompat.setContentIntent(mPendingIntent);
findViewById(R.id.mainButton1).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View p1)
{
//设置进度条为不确定的
notificationCompat.setProgress(100, 0, true);
//设置为正在进行时的Notification(用户无法取消这条通知)
notificationCompat.setOngoing(true);
//设置内容
notificationCompat.setContentText("正在准备中");
//显示这条通知,id为0
notificationManager.notify(0, notificationCompat.build());
//让主线程体眠2秒后,再去执行循环
//模拟网络延迟操作
Thread.sleep(2000);
for (int i = 0;i <= 100;i = i + 10)
{
//设置进度值
notificationCompat.setProgress(100, i, false);
//设置内容
notificationCompat.setContentText("已下载" + i + "%");
//显示通知,id为0
//如果已有一条id为0的通知,则会覆盖上一条id为0的通知,否则就会重新创建一条通知
notificationManager.notify(0, notificationCompat.build());
//每次循环结束,先体眠200ms,再执行下次循环
//模拟下载
Thread.sleep(200);
}
//设置为非正在进行中的通知(用户可以取消这条通知)
notificationCompat.setOngoing(false);
//设置内容
notificationCompat.setContentText("已下载完成");
//先体眠500ms后,再去显示通知
//去务必加上这句(不然会出错)
Thread.sleep(500);
//显示通知
notificationManager.notify(0, notificationCompat.build());
//使用toast提示用户已完成
Toast.makeText(MainActivity.this, "已下载完成", Toast.LENGTH_LONG).show();
}
});
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
效果图:
设置进度条方法解析
setProgress(int max, int progress, boolean indeterminate)
- 1
参数 | 解析 |
---|---|
max | 最大进度值 |
progress | 当前进度值 |
indeterminate | 是否为正在进行中的通知 |
- 面对我们的需求,仅仅只有系统默认样式的Notification肯定是无法满足我们的
- 我们需要自定义Notification的样式,才能设计出更精美的App
- 那么…接下来就让我们开始学习吧!
自定义通知样式
public class MainActivity extends AppCompatActivity
{
private NotificationManager notificationManager;
private NotificationCompat.Builder notificationCompat;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent mIntent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://m.baidu.com"));
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, mIntent, 0);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationCompat = new NotificationCompat.Builder(this);
//实例化RemoteViews对象,传入当前应用的包名和自定义的通知样式布局
RemoteViews mRemoteViews=new RemoteViews(getPackageName(), R.layout.layout_notification);
//设置imageview的显示的图片
mRemoteViews.setImageViewResource(R.id.layout_notificationImageView1, R.drawable.ic_launcher);
//设置按钮的文本
mRemoteViews.setTextViewText(R.id.layout_notificationButton1, "进入");
//为按钮设置点击事件
mRemoteViews.setOnClickPendingIntent(R.id.layout_notificationButton1, pendingIntent);
//(必须)一般不会调用,只有当我们自定义的通知样式出现问题时,才会调用。
notificationCompat.setSmallIcon(R.drawable.ic_launcher);
notificationCompat.setContentText("内容");
notificationCompat.setContentTitle("标题");
notificationCompat.setContent(mRemoteViews);
//点击空白(白色)区域触发
//notificationCompat.setContentIntent(pendingIntent);
findViewById(R.id.mainButton1).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View p1)
{
notificationManager.notify(0, notificationCompat.build());
}
});
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
其他方法
- 点击后自动取消通知
notificationCompat.setAutoCancel(true);
- 1
- 取消一条通知
notificationManager.cancel(通知的id);
- 1
- 取消所有通知
notificationManager.cancelAll();
- 1
- 首次出现时会从屏幕上方出现
notificationCompat.setTicker("");
- 1
- 显示一张大图
notificationCompat.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher)));
- 1