关于 Notification.setLatestEventInfo已经废弃不能再使用了,以后将用Notification.Builder builder = new Notification.Builder(this);所代替
//获取系统的NotificationManager服务
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder.setNumber(count);当发送多个notification,在状态栏中的对应条目中显示个数
//为Notification设置图标,该图标显示在状态栏
builder.setSmallIcon(R.mipmap.ic_launcher);
//为Notification设置文本内容,该文本显示在状态栏,当接收到消息时,在状态栏滚动时显示的ticker
builder.setTicker("启动其他activity");
//为Notification设置发送时间,系统时间
builder.setWhen(System.currentTimeMillis());
//下拉菜单显示的具体的内容
Notification notification = builder.setContentIntent(pendingIntent).setContentTitle("title").setContentText("text").build();
-
intent英文意思是意图,pending表示即将发生或来临的事情。
PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。 -
Intent是及时启动,intent随所在的activity消失而消失。
-
PendingIntent可以看作是对intent的包装,通常通过getActivity,getBroadcast,getService来得到pendingintent的实例,
-
当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。
-
正由于pendingintent中保存有当前App的Context,使它赋予外部App一种能力,
-
使得外部App可以如同当前App一样的执行pendingintent里的Intent,就算在执行时当前App已经不存在了,
-
也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。
-
常和alermanger和notificationmanager一起使用。
-
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,
-
而Pendingintent,一般用在Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。
public class LaunchActivity extends AppCompatActivity{
int count =0;
static final int NOTIFICATION_ID=0x1123;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
Button delete = (Button) findViewById(R.id.delete);
button.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
count++;
//创建一个启动其他Activity的Intent
Intent intent = new Intent(LaunchActivity.this,OtherActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(LaunchActivity.this,0,intent,0);
Notification.Builder builder = new Notification.Builder(LaunchActivity.this);
//在状态栏显示未读的个数
builder.setNumber(count);
intent.putExtra("name","name:"+count);
//为Notification设置图标,该图标显示在状态栏
builder.setSmallIcon(R.mipmap.ic_launcher);
//为Notification设置文本内容,该文本显示在状态栏,当接收到消息时,在状态栏滚动时显示的ticker
builder.setTicker("启动其他activity");
//为Notification设置发送时间,系统时间
builder.setWhen(System.currentTimeMillis());
//设置声音
builder.setDefaults(Notification.DEFAULT_SOUND);
//默认声音,震动,默认闪光灯
builder.setDefaults(Notification.DEFAULT_ALL);
//下拉菜单显示的具体的内容
Notification notification = builder.setContentIntent(pendingIntent).setContentTitle("title").setContentText("text").build();
//获取系统的NotificationManager服务
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//发送通知,id做标示,取消对应的通知会用到
notificationManager.notify(NOTIFICATION_ID, notification);
}
});
在OtherActivity中,
OtherActivity
public class OtherActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView= new TextView(this);
textView.setText("我是另一个activity");
textView.setTextSize(60);
setContentView(textView);
/**
* 当用户点击notification跳转到activity时,去掉状态栏的显示
*/
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}
}
如果,每次发送一个notification,都想单独的显示在状态栏中,像listview列表,则只需要在notificationManager.notify(int
ID,
notification); 改变id值即可蓝色指示框所代表的的意思如下:
1.标题
2.大图标
3.通知内容
4.通知数据
5.小图标
6.Notification的发布时间。可以通过调用setWhen()设置一个明确的时间,默认是系统收到该notification的时间。
首先将notification的一些UI信息以及相关动作赋予NotificationCompat.Builder对象,然后通过NotificationCompat.Builder.build()来获得notification对象自己;然后调用NotificationManager.notify()向系统转交这个通知。一个notification对象需要包含如下内容:
小图标(setSmallIcon()获取)
标题(setContentTitle()获取)
详情文字(setContentText()获取)
除此之外,其余内容都是可选的,要了解这些可选的内容参考NotificationCompat.Builder的文档。
Notification的动作与行为
虽然这也是可选的,但是你还是应该为你的notification至少添加一种行为:允许用户通过点击notification进入一个activity中进行更多的查看或者后续操作。一个notification可以提供多种动作,而且你也应该让用户点击一个notification之后能总是有相应的响应动作,通常是打开一个activity。你还可以在notification中添加能响应点击事件的button,比如延迟一下闹钟,或者立即回复一条短消息。
在notification内部,一个动作本身是被定义在一个PendingIntent中,PendingIntent包含了一个用于启动你app中activity的intent。要将PendingIntent和一个手势联系起来,你需要调用合适的NotificationCompat.Builder方法。比如你想在点击notification文字的时候启动activity,你需要调用NotificationCompat.Builder的setContentIntent()来添加PendingIntent。启动一个activity是notification动作响应中最普遍的一类。
notification = new Notification.Builder(MainActivity.this).setContentTitle(getString(R.string.app_name1))
《设置多行,在状态栏会分行显示》
.setStyle(new Notification.BigTextStyle().bigText(getString(R.string.app_name2)))
.setTicker(getString(R.string.app_name1))
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();