Android 简单 通知栏 Notification

Notificaiton状态通知栏:主要是用在消息的提醒,类似于QQ,微信信息来的时候在通知栏弹出一个通知。可以有点击效果。

首先我们先获得通知栏的服务实例。

NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
这个服务实例对象负责发通知,清除通知

然后我们就进行通知栏构造器的构造

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.wsy2)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentText("弹出的信息文本")
                .setOngoing(true)
                .setDefaults(Notification.DEFAULT_VIBRATE);
接着我们可以使用意图的方式来进行一些操作(如跳转到那个Activity里面去)

Intent backIntent = new Intent(this, ScreenRecordActivity.class);
        backIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
最后通过PendingIntent(和Intent略有不同,它可以设置执行次数,主要用于远程服务通信、闹铃、通知、启动器、短信中,在一般情况下用的比较少。)
PendingIntent pendingIntent = PendingIntent.getActivity(this, PENDING_REQUEST_CODE, backIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotificationManager.notify(NOTIFICATION_ID, builder.build());



深入了解到http://blog.csdn.net/vipzjyno1/article/details/25248021/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章