日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 通知写法_Android消息通知-Notification

發(fā)布時間:2023/12/16 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 通知写法_Android消息通知-Notification 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Android中常用的消息提醒,一種是Toast彈出提醒內(nèi)容,一種是AlterDialog彈出框來提醒用戶,還有一種就是消息通知的,用Android經(jīng)常收到各種通知就是Notifation。Notification是一種具有全局效果的通知,展示在屏幕頂端,表現(xiàn)一個圖標(biāo)的形式,當(dāng)用戶向下滑動的時候,展示出通知具體的內(nèi)容。

Notifation概念

Android很多東西存在版本兼容性問題Android3.0是一個之前在其之前構(gòu)建Notification推薦使用Notification.Builder構(gòu)建,Android3.0之后,一般推薦使用NotificationCompat.Builder構(gòu)建。通知一般通過NotificationManager服務(wù)來發(fā)送一個Notification對象來完成,NotificationManager是一個重要的系統(tǒng)級服務(wù),該對象位于應(yīng)用程序的框架層中,應(yīng)用程序可以通過它像系統(tǒng)發(fā)送全局的通知。

這個時候可以先看看要實現(xiàn)的效果:

Demo實現(xiàn)

關(guān)于Notifation網(wǎng)上也有很多例子,我就寫一個簡單的,三個按鈕,一個發(fā)送,一個取消,還有一個就是新版本的寫法,看下App:

通知事件的寫法,點擊之后的效果就是最開始看到的那張圖片:

notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.ic_launcher, "通知", System.currentTimeMillis());

//如果是已經(jīng)通知,自動消失

notification.flags = Notification.FLAG_AUTO_CANCEL;

//新建一個打電話的意圖

Intent intent = new Intent();

intent.setAction(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:120"));

//將上面的意圖組合到一起

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

notification.setLatestEventInfo(this, "XX彩票", "恭喜你中獎100萬", contentIntent);

notificationManager.notify(100, notification);

取消事件:(注意上面的那個通知的ID號是100):

notificationManager.cancel(100);

新版寫法直接調(diào)用Notifation中的Builder方法即可:

Notification notifation= new Notification.Builder(this)

.setContentTitle("小官巨腐")

.setContentText("現(xiàn)金1.2億,黃金37公斤")

.setSmallIcon(R.drawable.ic_launcher)

.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))

.build();

NotificationManager manger= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

manger.notify(0, notifation);

效果如下:

總結(jié)

以上是生活随笔為你收集整理的android 通知写法_Android消息通知-Notification的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。