Notification基本通知的两种写法
生活随笔
收集整理的這篇文章主要介紹了
Notification基本通知的两种写法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
private void newNotify() {// 1.創(chuàng)建通知的Builder對象NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);//2.設置參數 對象鏈式操作mBuilder.setSmallIcon(R.drawable.ic_launcher); //設置小圖標mBuilder.setContentTitle("hello title"); //設置標題mBuilder.setContentText("Hello content");//設置內容//3.創(chuàng)建一個意圖對象Intent resultIntent = new Intent(this, OtherActivity.class);//4.創(chuàng)建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.發(fā)送通知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(); // 發(fā)送通知的時間// 定義意圖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.發(fā)通知manager.notify(1, notification);}
總結
以上是生活随笔為你收集整理的Notification基本通知的两种写法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序流程图作业
- 下一篇: 手写实现乞丐版mybatis