原文:Xamarin.Android開發實踐(六)html
在平常的app應用中常常須要使用通知,由於服務、廣播後臺活動若是有事件須要通知用戶,則須要經過通知欄顯示,而在Xamarin.Android下的通知須要獲取NotificationManager服務,而該服務須要經過GetSystemService獲取,同時還要傳遞一個標識符。獲取了通知管理器後咱們就能夠實例化Notification,而後再由NotificationManager發送出去。這就是整個過程了。下面咱們將一一詳解通知。android
爲了下面的學習和演示咱們須要作好一些前期的準備數組
1.打開Main.axml刪除上面的控件app
2.打開MainActivity.cs文件並寫入如下內容ide
1 [Activity(Label = "NotificationStudy", MainLauncher = true, Icon = "@drawable/icon")] 2 public class MainActivity : Activity 3 { 4 private NotificationManager nMgr; 5 6 protected override void OnCreate(Bundle bundle) 7 { 8 base.OnCreate(bundle); 9 SetContentView(Resource.Layout.Main); 10 11 //獲取通知管理類 12 nMgr = (NotificationManager)GetSystemService(NotificationService);
3.右擊項目-》屬性而後按照以下所示加上可以控制振動的權限post
首先咱們打開Main.axml文件,而後設置以下一個按鈕:學習
並設置id爲@+id/normalButton測試
在MainActivity.cs先獲取按鈕對象:ui
1 Button normalButton = FindViewById<Button>(Resource.Id.normalButton);
監聽normalButton按鈕的點擊事件:this
1 normalButton.Click += (e, s) =>
2 { 3 //設置通知的圖標以及顯示的簡介Title 4 Notification notify = new Notification(Resource.Drawable.Icon, "普統統知"); 5 //初始化點擊通知後打開的活動 6 PendingIntent pintent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), PendingIntentFlags.UpdateCurrent); 7 //設置通知的主體 8 notify.SetLatestEventInfo(this, "普統統知標題", "普統統知內容", pintent); 9 //發送通知 10 nMgr.Notify(0, notify); 11 };
其中咱們先實例化了一個Notification對象,並設置其圖標以及Ticker文字:
1 Notification notify = new Notification(Resource.Drawable.Icon, "普統統知");
固然衆所周知當咱們點擊通知以後都會打開對應的活動,因此咱們須要初始化一個延遲意圖,以便通知能夠打開:
1 PendingIntent pintent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), PendingIntentFlags.UpdateCurrent);
這個PendingIntent僅僅只是Intent的一個封裝。最後是設置通知的標題和內容以及對應的活動,最後就是發送這個通知,再發送通知的Notify方法中第一個參數爲該通知的ID,有了這個ID後面就能夠取消這個通知。
下面咱們測試,發送該通知:
經過上面的代碼咱們已經發送了一個通知,那麼咱們還須要關閉這個通知,這裏咱們再在Main.axml中添加一個按鈕:
並設置其id爲@+id/cancelNotifyButton
打開MainActivity.cs文件,並綁定監聽事件:
1 Button cancelNotifyButton = FindViewById<Button>(Resource.Id.cancelNotifyButton); 2 cancelNotifyButton.Click += (e, s) => 3 { 4 //根據id取消通知 5 nMgr.Cancel(0); 6 };
而後發送一個通知後,點擊取消普統統知,會發現通知欄中不存在咱們發送的通知了。
首先咱們仍是要在Main.axml中添加一個按鈕:
並設置它的id爲@+id/lsvButton
打開MainActivity.cs並寫入以下代碼:
1 Button lsvButton = FindViewById<Button>(Resource.Id.lsvButton); 2 lsvButton.Click += (e, s) => 3 { 4 Notification notify = new Notification(Resource.Drawable.Icon, "帶有聲音、LED光和震動的通知"); 5 //設置該通知具備聲音、LED光和震動 6 notify.Defaults = NotificationDefaults.All; 7 8 //獲取系統默認的通知聲音 9 Android.Net.Uri ringUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification); 10 //設置通知的聲音 11 notify.Sound = ringUri; 12 13 //設置一秒的震動 14 notify.Vibrate = new long[] { 1000 }; 15 16 //設置LED的顏色爲綠色 17 notify.LedARGB = Color.Green; 18 //設置LED顯示時間爲1s 19 notify.LedOnMS = 1000; 20 //設置LED熄滅時間爲1s 21 notify.LedOffMS = 1000; 22 //設置標誌位,不然沒法顯示LED 23 notify.Flags = NotificationFlags.ShowLights | notify.Flags; 24 25 PendingIntent pintent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), 0); 26 27 notify.SetLatestEventInfo(this, "標題", "內容", pintent); 28 29 nMgr.Notify(1, notify); 30 };
下面咱們來分析一下代碼,既然這個通知帶有聲音等,那麼咱們須要設置Defaults:
1 notify.Defaults = NotificationDefaults.All;
由於筆者使用了全部,因此直接是All,固然還能夠是Sound、Vibrate和Lights的組合
爲了可以貼近系統,因此咱們並無設置個性的聲音,而是獲取了系統自己的通知聲音,下面的代碼就是獲取代碼:
1 Android.Net.Uri ringUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
最後是將這個聲音賦給通知:
1 notify.Sound = ringUri;
而後就是震動:
1 notify.Vibrate = new long[] { 1000 };
筆者設置的是震動一秒,固然這是一個數組。而規則就是 震動的毫秒數,靜止的毫秒數,震動的毫秒數…這種規則
最後就是比較麻煩的LED,首先是指定LED燈的顏色(若是不存在該顏色,系統會選擇臨近的顏色):
1 notify.LedARGB = Color.Green;
而後筆者設置了顯示的毫秒數與熄滅的毫秒數:
1 //設置LED顯示時間爲1s 2 notify.LedOnMS = 1000; 3 //設置LED熄滅時間爲1s 4 notify.LedOffMS = 1000;
爲了可以確保LED可以顯示咱們還須要設置Flags:
1 notify.Flags = NotificationFlags.ShowLights | notify.Flags;
最後發送通知(模擬器上看不出來,建議真機測試)
這個方式相對於Notification更簡單,也更快捷,可是隻有在Android 3.0以上才支持(面對現在都是Android 4.0以上應該沒有問題了)
先在Main.axml中添加對應的按鈕:
並設置對應的id爲@+id/builderButton
最後就是MainActivity.cs中的代碼:
1 Button builderButton = FindViewById<Button>(Resource.Id.builderButton); 2 builderButton.Click += (e, s) => 3 { 4 var pintent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), 0); 5 6 var notify = new Notification.Builder(this) 7 .SetTicker("來自Builder的通知") //設置通知的簡介文字 8 .SetSmallIcon(Resource.Drawable.Icon) //設置通知的圖標 9 .SetDefaults(NotificationDefaults.All) //設置該通知具有聲音、LED和震動 10 .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification)) //設置通知聲音 11 .SetVibrate(new long[] { 1000 }) //設置震動頻率 12 .SetLights(Color.Red, 1, 0) //設置LED 13 .SetContentTitle("標題") //設置標題 14 .SetContentText("內容") //設置內容 15 .SetContentInfo("信息") //設置右下角顯示的文字 16 .SetAutoCancel(true) //點擊該通知後是否自動消失 17 //經過 SetLargeIcon 能夠設置大圖標 18 .SetContentIntent(pintent); 19 20 nMgr.Notify(3, notify.Notification); 21 };
這裏咱們能夠看到經過Builder方式建立一個通知是多麼的方便,只要經過SetXXXXX就能夠完成了,最後只要在發送通知的時候傳入其Notification屬性便可,固然這裏不少的方法跟經過Nitification是同樣的就不單獨解釋了,並且也有註釋說明。
你們在下載文件的時候,都會看到通知欄會有當前下載任務的進度,而這個通知能夠經過Builder方式實現,並且更快捷。
在Main.axml中添加對應的按鈕
並設置id爲@+id/builderButton
其次就是MainActivity.cs文件
1 Button progressButton = FindViewById<Button>(Resource.Id.progressButton); 2 progressButton.Click += (e, s) => 3 { 4 var notify = new Notification.Builder(this) 5 .SetTicker("進度條通知") 6 .SetSmallIcon(Resource.Drawable.Icon) 7 .SetOngoing(true) //設置該通知是否能夠被用戶移除 true 表示不能夠 8 .SetNumber(2) //設置該同時的條數 會在右下角顯示數量 9 .SetContentTitle("標題") 10 .SetProgress(100, 50, true); //第三個參數若是設置爲true則進度條變成不肯定進度條 11 12 nMgr.Notify(4, notify.Notification); 13 };
這裏主要使用了SetProgress方法來設置進度條的最大值,當前值。最後一個參數設置爲true則表示該進度條爲不肯定進度條,就是隻會顯示滑動,不會顯示當前的進度。
1 SetProgress(100, 50, true);
這裏咱們還使用了一個新方法SetOngoing,經過前面的通知,你們會發現這些通知用戶徹底能夠經過手動的方式移除掉,可是咱們如何建立一個用戶沒法移除的通知呢?就是經過使用SetOngoing方法,並傳入一個true便可。
下面爲實際運行圖:
你們在使用音樂相關的應用的時候會發現通知欄會有一個簡單的功能界面,有當前歌曲 的名稱,專輯圖片和暫停,下一曲等按鈕,這些固然不是通知自帶的,而是經過自定義通知來實現的,而本節咱們不只僅會學習如何使用自定義通知,同時還會學習 如何設置自定義通知中控件的值,以及綁定監聽事件。
首先咱們在Main.axml中添加按鈕
設置其id爲@+id/customeViewButton
既然是自定義視圖,固然還須要一個視圖,因此咱們在Resources/layout下新建一個視圖,並命名爲NotificationCustomeView,並在其中寫入以下的xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android" 3 p1:minWidth="25px" 4 p1:minHeight="25px" 5 p1:layout_width="match_parent" 6 p1:layout_height="match_parent" 7 p1:id="@+id/relativeLayout1" 8 p1:padding="5dp"> 9 <ImageView 10 p1:src="@drawable/Icon" 11 p1:layout_width="wrap_content" 12 p1:layout_height="match_parent" 13 p1:id="@+id/imageView1" /> 14 <RelativeLayout 15 p1:minWidth="25px" 16 p1:minHeight="25px" 17 p1:layout_width="match_parent" 18 p1:layout_height="match_parent" 19 p1:layout_toRightOf="@id/imageView1" 20 p1:id="@+id/relativeLayout2" 21 p1:paddingLeft="10dp"> 22 <TextView 23 p1:text="Large Text" 24 p1:textAppearance="?android:attr/textAppearanceLarge" 25 p1:layout_width="match_parent" 26 p1:layout_height="wrap_content" 27 p1:id="@+id/ncvTextView" /> 28 <ProgressBar 29 style="?android:attr/progressBarStyleHorizontal" 30 p1:layout_width="match_parent" 31 p1:layout_height="match_parent" 32 p1:layout_below="@id/ncvTextView" 33 p1:id="@+id/ncvProgressBar" 34 p1:indeterminateOnly="false" 35 p1:indeterminate="false" /> 36 </RelativeLayout> 37 </RelativeLayout>
固然最終的結果圖以下所示:
打開MainActivity.cs文件
1 Button customeViewButton = FindViewById<Button>(Resource.Id.customeViewButton); 2 customeViewButton.Click += (e, s) => 3 { 4 //初始化自定義視圖 5 var customeView = new RemoteViews(this.PackageName, Resource.Layout.NotificationCustomeView); 6 7 var notify = new Notification.Builder(this) 8 .SetTicker("自定義視圖通知") 9 .SetSmallIcon(Resource.Drawable.Icon) 10 .SetNumber(2) 11 .SetContent(customeView); //設置通知的自定義視圖 12 13 //設置自定義視圖中textview的文字 14 notify.Notification.ContentView.SetTextViewText(Resource.Id.ncvTextView, "經過代碼修改"); 15 16 //設置自定義視圖中Progressbar的進度 17 notify.Notification.ContentView.SetProgressBar(Resource.Id.ncvProgressBar, 100, 40, false); 18 19 //給自定義視圖中的ProgressBar綁定事件 20 var pIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), 0); 21 //綁定事件 22 notify.Notification.ContentView.SetOnClickPendingIntent(Resource.Id.ncvProgressBar, pIntent); 23 24 nMgr.Notify(5, notify.Notification); 25 };
首先咱們須要實例化一個RemoteViews封裝自定義視圖:
1 var customeView = new RemoteViews(this.PackageName, Resource.Layout.NotificationCustomeView);
而後經過通知的SetContent將其傳入
1 SetContent(customeView)
下面咱們還須要訪問自定義視圖中的控件並設置他們的值,這裏咱們須要使用ContentView的Setxxxxxx來設置,以下下面咱們就設置了TextView的文字和ProgressBar的進度:
1 //設置自定義視圖中textview的文字 2 notify.Notification.ContentView.SetTextViewText(Resource.Id.ncvTextView, "經過代碼修改"); 3 4 //設置自定義視圖中Progressbar的進度 5 notify.Notification.ContentView.SetProgressBar(Resource.Id.ncvProgressBar, 100, 40, false);
最後就是綁定事件,通知裏面的綁定事件不一樣於其餘的,只能將一個意圖與這個事件綁定(實際運用中也應該如此,好比你點擊了暫停按鈕,將會經過意圖傳遞對應的參數到服務中從而中止播放)
1 notify.Notification.ContentView.SetOnClickPendingIntent(Resource.Id.ncvProgressBar, pIntent);
下面爲實際的運行圖