[android] 01_Notification用户提醒

Android 4.0

Notification用户提醒
notification

1、PendingIntent继承Parctable,是可以在进程之间之间通信的,notification显示在systemui上的。
2、FLAG_NO_CLEAR,通知不可被清除,流氓软件,作死的弹,
可以在应用管理,强行关闭该程序,就会消失了,android4.0新特性,显示通知,
去掉,该程序就再也不能弹出来了



     The callouts in the illustration refer to the following:

  1. Content title  内容标题
  2. Large icon 大图标
  3. Content text 内容文本
  4. Content info 内容信息
  5. Small icon 小图标
  6. Time that the notification was issued. You can set an explicit value with setWhen(); if you don't it defaults to the time that the system received the notification.     显示时间
核心代码:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    /**
     * 单击弹出提醒
     * @param view
     */

    @SuppressLint("NewApi")
    public void notify(View view) {
 
        // 1.获取一个notification的管理器
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
 
        // TODO 1、android api11 以上可以用 。链式调用,因为返回的都是Builder
        /*Notification noti = new Notification.Builder(getApplicationContext())
                .setContentTitle("我是标题信息")
                .setContentText("我是内容信息")
                .setSmallIcon(R.drawable.notification)//如果没有指定大的,小的会自动移动到大的图标上去
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
                .build();
 
        notificationManager.notify(0, noti);// 如果id相同,新来的提醒会覆盖原来的提醒*/

 
        // TODO 2、过时的写法,更好的兼容前面的版本
        Notification notification = new Notification(R.drawable.notification,"我是滚动提醒内容", System.currentTimeMillis());
 
        // notification.flags = Notification.FLAG_AUTO_CANCEL;// 自动取消当用户点击了它
        notification.flags = Notification.FLAG_NO_CLEAR;// 不会消失
 
        // 设置notification的具体信息 延期的意图 让另外一个应用程序 执行的意图
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:110"));
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(this, "我是标题信息", "我是内容信息", contentIntent);
        notificationManager.notify(0, notification);
    }
}
结果:
新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);
    }
}
结果:
①状态栏
②下拉菜单中