Notification基本通知的两种写法

来源:互联网 发布:数据保全系统 编辑:程序博客网 时间:2024/05/29 04:48
private void newNotify() {        // 1.创建通知的Builder对象        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(                this);        //2.设置参数   对象链式操作        mBuilder.setSmallIcon(R.drawable.ic_launcher); //设置小图标        mBuilder.setContentTitle("hello title"); //设置标题        mBuilder.setContentText("Hello content");//设置内容        //3.创建一个意图对象        Intent resultIntent = new Intent(this, OtherActivity.class);        //4.创建TaskStackBuilder对象        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);        //5.添加到stackBuilder对象中        stackBuilder.addParentStack(OtherActivity.class);        //6.添加到顶端        stackBuilder.addNextIntent(resultIntent);        //7.意图对象        PendingIntent resultPendingIntent =                stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);        //8.设置意图对象        mBuilder.setContentIntent(resultPendingIntent);        // 9.获取NotificationManager对象       NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        //10.发送通知       mNotificationManager.notify(mId, mBuilder.build());    }    private void oldNotify() {        // 1.获取NotificationManager对象        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        // 2.定义通知        Notification notification = new Notification();        // 3.设置参数        notification.icon = R.drawable.ic_launcher; // 设置图标        notification.when = System.currentTimeMillis(); // 发送通知的时间        // 定义意图        Intent intent = new Intent(this, OtherActivity.class);        // 意图 :跨进程的意图        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,                intent, PendingIntent.FLAG_UPDATE_CURRENT);        // 设置通知的最新事件消息        notification.setLatestEventInfo(this, "hello title", "hello content",                pendingIntent);        // 3.发通知        manager.notify(1, notification);    }
2 0
原创粉丝点击