记得要导入包哦!
compile 'com.android.support:support-v4:+'
实例:
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());
}
});
}
}
怎么回事?怎么和QQ的效果不一样呢?别着急,我们继续往下看,在里面再添加这两段代码,然后我们再试试!
notificationCompat.setPriority(NotificationCompat.PRIORITY_MAX);
notificationCompat.setDefaults(NotificationCompat.DEFAULT_ALL);
效果图:
嘿嘿,已经可以了,接下来,让我们为它设置一个点击事件吧!
添加下面的两段代码
Intent in=new Intent(Intent.ACTION_VIEW,Uri.parse("https://m.baidu.com"));
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,in,0);
//为Notification整体设置一个点击事件
notificationCompat.setContentIntent(pendingIntent);
完整代码:
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());
}
});
}
}
通知栏还支持设置一个进度条!
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();
}
});
}
}
setProgress(int max, int progress, boolean indeterminate)
参数 | 解析 |
---|---|
max | 最大进度值 |
progress | 当前进度值 |
indeterminate | 是否为正在进行中的通知 |
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());
}
});
}
}
notificationCompat.setAutoCancel(true);
notificationManager.cancel(通知的id);
notificationManager.cancelAll();
notificationCompat.setTicker("");
notificationCompat.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher)));