Android中使用Notification實現普通通知欄(Notification樣本一)

來源:互聯網
上載者:User

標籤:ica   admin   點擊   remote   頁面   .com   資料   support   instance   

Notification是在你的應用常規介面之外展示的訊息。當app讓系統發送一個訊息的時候,訊息首先以圖表的形式顯示在通知欄。要查看訊息的詳情需要進入通知抽屜(notificationdrawer)中查看。(notificationdrawer)都是系統層面控制的,你可以隨時查看,不限制於app。

Notification的設計:

作為android UI中很重要的組成部分,notification擁有專屬於自己的設計準則。

Notification的介面元素在通知抽屜中的notification有兩種顯示方式,取決於你的android版本以及notificationdrawer的狀態。

Notification的兩種顯示方式:

(1)普通視圖

這種風格是notification drawer的標準顯示方式。

(2)寬視圖

指你的notification被展開的時候會顯示更大的視圖,這種風格是android4.1之後才有的新特性。

下面我們詳細介紹普通視圖的實現:

在圖通視圖中,notification最高64dp,即使你建立了一個寬視圖風格的notification,在未展開的情況下也是以普通大小顯示出來。下面是一個普通的notification。

藍色指示框所代表的的意思如下:

1.標題

2.大表徵圖

3.通知內容

4.通知數據

5.小表徵圖

6.Notification的發布時間。

可以通過調用setWhen()設定一個明確的時間,

預設是系統收到該notification的時間。

下面我們是我們本次的示範效果:

本次在普通視圖的基礎上添加了點擊頁面跳轉的效果,可以理解為添加Notification的動作與行為:

雖然這也是可選的,但是你還是應該為你的notification至少添加一種行為:允許使用者通過點擊notification進入一個activity中進行更多的查看或者後續操作。一個notification可以提供多種動作,而且你也應該讓使用者點擊一個notification之後能總是有相應的響應動作,通常是開啟一個activity。你還可以在notification中添加能響應點擊事件的button,比如延遲一下鬧鐘,或者立即回複一條短訊息。

在notification內部,一個動作本身是被定義在一個PendingIntent中,PendingIntent包含了一個用於啟動你app中activity的intent。要將PendingIntent和一個手勢聯絡起來,你需要調用合適的NotificationCompat.Builder方法。

比如你想在點擊notification文字的時候啟動activity,你需要調用NotificationCompat.Builder的setContentIntent()來添加PendingIntent。啟動一個activity是notification動作響應中最普遍的一類。

第一步:Layout中 的activity_main.xml(僅設定觸發按鈕):
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3     xmlns:android="http://schemas.android.com/apk/res/android" 4     xmlns:tools="http://schemas.android.com/tools" 5     android:id="@+id/activity_main" 6     android:layout_width="match_parent" 7     android:layout_height="match_parent" 8     tools:context="com.example.administrator.day12.MainActivity"> 9     <Button10         android:text="顯示通知"11         android:layout_width="match_parent"12         android:layout_height="wrap_content"13         android:id="@+id/button"14         android:onClick="show1" />15 </LinearLayout>
第二步:Layout中 跳轉頁面activity_content.xml(僅設定顯示文本):
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     xmlns:tools="http://schemas.android.com/tools" 4     android:id="@+id/activity_content" 5     android:layout_width="match_parent" 6     android:layout_height="match_parent" 7     tools:context="com.example.administrator.day12.ContentActivity"> 8     <TextView 9         android:layout_width="match_parent"10         android:layout_height="match_parent"11         android:gravity="center"12         android:textSize="30sp"13         android:text="十勝十敗" />14 </LinearLayout>
第三步: java(主介面按鈕的點擊事件)實現代碼MainActivity.java:
 1 import android.app.Notification; 2 import android.app.NotificationManager; 3 import android.app.PendingIntent; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.graphics.BitmapFactory; 7 import android.support.v7.app.AppCompatActivity; 8 import android.os.Bundle; 9 import android.support.v7.app.NotificationCompat;10 import android.view.View;11 import android.widget.RemoteViews;12 public class MainActivity extends AppCompatActivity {13     private static final int NO_1 =0x1 ;14     @Override15     protected void onCreate(Bundle savedInstanceState) {16         super.onCreate(savedInstanceState);17         setContentView(R.layout.activity_main);18     }19     public  void show1(View v){20         NotificationCompat.Builder builder = new NotificationCompat.Builder(this);21         builder.setSmallIcon(R.mipmap.guojia);22         builder.setContentTitle("郭嘉");23         builder.setContentText("我們打袁紹吧");24         //設定Notification.Default_ALL(預設啟用全部服務(呼吸燈,鈴聲等)25         builder.setDefaults(Notification.DEFAULT_ALL);26         //調用NotificationCompat.Builder的setContentIntent()來添加PendingIntent27         Intent intent = new Intent(this, ContentActivity.class);28         intent.putExtra("info", "郭嘉給你發了一個計策!");29         PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);30         builder.setContentIntent(pi);31         //擷取Notification32         Notification n = builder.build();33         //通過NotificationCompat.Builder.build()來獲得notification對象自己34         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);35         //然後調用NotificationManager.notify()向系統轉交36         manager.notify(NO_1, n);37     }38 }

 

第四步: java(跳轉後Activity)功能代碼實現ContentActivity.java(只土司):
1 public class ContentActivity extends AppCompatActivity {2     @Override3     protected void onCreate(Bundle savedInstanceState) {4         super.onCreate(savedInstanceState);5         setContentView(R.layout.activity_content);6         //通過擷取MainActivity中設定的putExtra擷取土司內容7         Toast.makeText(this, getIntent().getStringExtra("info"), Toast.LENGTH_SHORT).show();8     }9 }

 

示範效果的代碼就這些,我們梳理下本次實現的思路:

(1)通過按鈕觸發點擊事件

(2)將notification的一些UI資訊以及相關動作賦予NotificationCompat.Builder對象,然後通過NotificationCompat.Builder.build()來獲得notification對象自己;然後調用NotificationManager.notify()向系統轉交這個通知。

(3)在第二步中通過Builder的setContentIntent()來添加PendingIntent,為Notification添加行為,也就是Activity的跳轉

(4)對開啟的Activity設定表現的效果。

 

Android中使用Notification實現普通通知欄(Notification樣本一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.