Notification用户提醒
notification
1、PendingIntent继承Parctable,是可以在进程之间之间通信的,notification显示在systemui上的。 2、FLAG_NO_CLEAR,通知不可被清除,流氓软件,作死的弹, 可以在应用管理,强行关闭该程序,就会消失了,android4.0新特性,显示通知, 去掉,该程序就再也不能弹出来了 The callouts in the illustration refer to the following:
|
核心代码:
|
结果: 新api: 老api: |
package cn.zengfansheng.day10.notification;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import cn.zengfansheng.day10.R;
public class NotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
}
@SuppressLint("NewApi")
// @SuppressWarnings("deprecation")
public void notification(View view) {
NotificationManager nManager = (NotificationManager) this
.getSystemService(NOTIFICATION_SERVICE);
Bitmap aBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.notification);
Notification notification = new Notification.Builder(this)
.setContentTitle("content title") //contentTitle
.setContentText("content text") //contentText
.setSmallIcon(R.drawable.ic_launcher) // 小图标
.setLargeIcon(aBitmap) // 大图标
.build();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + 110));
PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//
notification.contentIntent = contentIntent;// 该提醒被点击时,执行的intent
notification.tickerText = "qq邮件来了";// 状态栏滚动的文本
notification.flags = Notification.FLAG_AUTO_CANCEL;// 点击后,该提醒自动消失了 // NOTES==================== 过时的写法,兼容旧版本==============
// Notification notification = new Notification(R.drawable.notification,
// "我是滚动提醒内容", System.currentTimeMillis());// 状态栏图标,状态栏提示内容,显示的时间
//
// CharSequence contentTitle = "内容标题"; // 内容标题
// CharSequence contentText = "内容文本"; // 内容文本
//
// // 点击时打开的意图
// Intent intent = new Intent();
// intent.setAction(Intent.ACTION_DIAL);
// intent.setData(Uri.parse("tel:" + 110));
//
// int flags = PendingIntent.FLAG_UPDATE_CURRENT;
// PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, flags);
//
// notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
nManager.notify(null, 0, notification);
}
} 结果: ①状态栏 ②下拉菜单中 |