android通知-Notification,-notification

來源:互聯網
上載者:User

android通知-Notification,-notification

android中,當app需要向發送一些通知,讓使用者注意到你想要告知的資訊時,可以用Notification.下面,就來討論一下,Notification的用法,我們從實際的小例子來進行學習。

1.建立一個項目,在layout布局裡寫兩個按鈕,一個用來開啟通知,一個用來關閉通知。下面直接上布局代碼。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    >    <Button        android:id="@+id/bt_up"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:clickable="true"        android:onClick="openNotify"        android:text="open" />    <Button        android:id="@+id/bt_down"        android:layout_below="@id/bt_up"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="closeNotify"        android:text="close" /></RelativeLayout>

然後就是代碼的實現了,還是直接上代碼,很簡單,相信大家一看就明白。

package com.example.demo;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.view.View;/** *  * @author jianww * 2015-12-16 * */public class MainActivity extends Activity {/** * 1.先建立通知管理器,得到通知服務 * 2.建立通知對象,發出一個通知。 * 3. *///1.建立通知管理器,private NotificationManager notifyManager;//2.聲明通知物件變數。private Notification notify;//3.建立意圖,當點擊通知時,開啟相應的意思對象,跳轉到對應的類。private Intent intent;/* * Intent 是及時啟動,intent 隨所在的activity 消失而消失。 PendingIntent 可以看作是對intent的封裝,通常通過getActivity,getBroadcast ,getService來得到pendingintent的執行個體,當前activity並不能馬上啟動它所包含的intent,而是在外部執行 pendingintent時,調用intent的。正由於pendingintent中 儲存有當前App的Context,使它賦予外部App一種能力,使得外部App可以如同當前App一樣的執行pendingintent裡的 Intent, 就算在執行時當前App已經不存在了,也能通過存在pendingintent裡的Context照樣執行Intent。另外還可以處理intent執行後的操作。常和alermanger 和notificationmanager一起使用。 Intent一般是用作Activity、Sercvice、BroadcastReceiver之間傳遞資料,而Pendingintent,一般用在 Notification上,可以理解為順延強制的intent,PendingIntent是對Intent一個封裝。本例用pendingIntent可以從通知中開啟要開啟的app中的對象。*/private PendingIntent pendIntent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);}//開啟通知public void openNotify(View v) {//建立通知對象執行個體,傳入預設的通知對象圖片,通知標題,通知發出時間。notify = new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());//建立意圖。此意圖不會立刻執行,只有當pendingIntent執行時,才會執行傳入裡面的意圖。intent = new Intent(getApplicationContext(),MainActivity.class);//得到pendintent對象執行個體。設定此延時意圖的標記。pendIntent = PendingIntent.getActivity(getApplicationContext(), 100, intent, 0);//設定通知的標題與內容。notify.setLatestEventInfo(getApplicationContext(), "通知", "通知的內容", pendIntent);//設定通知的標記為預設。notify.flags = Notification.FLAG_AUTO_CANCEL;//開始通知。notifyManager.notify(100, notify);}//關閉通知。public void closeNotify(View v) {//關閉通知。pendIntent.cancel();}}

  就是這麼簡單,只是用來複習一下基礎知識。^_^

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.