国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中通知Notification的使用方法

2019-12-12 05:34:11
字體:
來源:轉載
供稿:網友

每個使用Android手機的人應該對Android中的通知不陌生,下面我們就學習一下怎么使用Android中的通知。

一、通知的基本用法

活動、廣播接收器和服務中都可以創建通知,由于我們一般在程序進入后臺后才使用通知,所以真實場景中,一般很少在活動中創建通知。

1、第一行代碼上面介紹的創建通知的方法

//獲得通知管理器NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)//創建通知對象,參數依次為通知圖標、ticker(通知欄上一閃而過的信息)、通知創建時間Notification notification = new Notification(R.drawable. ic_launcher, "This is ticker text", System.currentTimeMillis());//設置通知布局,參數依次為Context,通知標題、通知正文、PindingIntent對象(點擊通知之后的事件處理)notification.setLatestEventInfo(this, "This is content title", "This is content text", null);//顯示通知,參數依次為唯一的id、通知對象manager.notify(1, notification);

注:上面的方法現在已經被廢棄,當API Level為11及之前時使用此方法 

2、APILevel高于11低于16的可以用下面的方法創建通知

//1、獲得通知管理器NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//創建Builder,設置屬性Notification.Builder builder = new Notification.Builder(this)    .setAutoCancel(true)    .setContentTitle("title")    .setContentText("describe")    .setSmallIcon(R.drawable.ic_launcher)    .setWhen(System.currentTimeMillis())    .setOngoing(true);//獲得Notification對象Notification notification = builder.getNotification();//顯示通知manager.notify(1, notification);

3、API Level在16及以上,使用下面的方法創建通知

//1、獲得通知管理器NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//創建Builder,設置屬性Notification notification = new Notification.Builder(this)    .setAutoCancel(true)    .setContentTitle("title")    .setContentText("describe")    .setSmallIcon(R.drawable.ic_launcher)    .setWhen(System.currentTimeMillis())    .setOngoing(true)    .build();//顯示通知manager.notify(1, notification);

二、響應通知的點擊事件

我們通過 PendingIntent對象響應容通知的點擊事件
 1、獲得PendingIntent對象

PendingIntent用來處理通知的“意圖”。我們需要先構造一個Intent對象,然后再通過PendingIntent.getActivity()、PendingIntent.gBroadcast()、PendingIntent.getService()來啟動執行不同的意圖。這三個靜態方法傳入的參數相同,第一個為Context,第二個參數一般傳入0,第三個參數為Intent對象,第四個參數指定PendingIntent的行為,有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT和FLAG_UPDATE_ CURRENT這四種值可選。 

2、設置PendingIntent

通過setContentIntent(pendingIntent)來設置。 

下面是一個簡單的例子

//獲得通知管理器NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//構造Intent對象Intent intent = new Intent(MainActivity.this, TestActivity.class);//獲得PendingIntent對象PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);//創建Builder,設置屬性Notification notification = new Notification.Builder(this)    .setAutoCancel(true)    .setContentTitle("title")    .setContentText("describe")    .setSmallIcon(R.drawable.ic_launcher)    .setContentIntent(pendingIntent)  //設置PendingIntent    .setWhen(System.currentTimeMillis())    .setOngoing(true)    .build();//顯示通知manager.notify(1, notification);

三、取消通知

取消通知只需要在cancel()方法中傳入我們創建通知時指定的id即可 

復制代碼 代碼如下:
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(1);

四、通知的高級用法

1、通知到來時播放音頻

Notification有一個屬性是sound,這里需要傳入音頻對應的URI

 //音頻UriUri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones"));setSound(soundUri);

2、通知到來時手機振動

我們使用vibrate這個屬性讓通知到來時控制手機振動。vibrate需要一個長整型數組,用于設置手機靜止和振動的時長,單位為毫秒。下標為偶數的表示手機靜止的時長,下標為奇數為手機振動的時長。

 //手機振動靜止設置(靜止0秒,振動一秒,靜止一秒,振動一秒)long[] vibrate = {0, 1000, 1000, 1000};setVibrate(vibrate)

注:控制手機還需要在AndroidManifest.xml中聲明權限:

<uses-permission android:name="android.permission.VIBRATE"/>

3、通知到來時閃爍LED燈

LED燈的使用涉及到以下一個屬性:
ledARGB ――- 控制LED燈的顏色
ledOnMS ――- 控制LED燈亮起的時間,以毫秒為單位
ledOffMS ――

主站蜘蛛池模板: 民和| 博湖县| 崇文区| 内丘县| 定南县| 宕昌县| 凤冈县| 久治县| 清镇市| 微山县| 延津县| 文化| 前郭尔| 腾冲县| 平邑县| 班玛县| 巴塘县| 鄂尔多斯市| 临江市| 正镶白旗| 金溪县| 吴忠市| 曲松县| 砀山县| 永顺县| 曲麻莱县| 彩票| 江阴市| 得荣县| 巧家县| 万州区| 嵊泗县| 凤庆县| 晋城| 临泉县| 江永县| 马尔康县| 合肥市| 鄂托克旗| 清涧县| 广西|