Android[初級教程]第十二章 Notification的應用

來源:互聯網
上載者:User

這一章節,我們來學習Notification的應用,很多人問Notification是什麼東東啊?我打個比方吧,還是以西遊記來說:唐僧被妖怪們抓住了,那悟空得知道是哪個妖怪抓住了他師傅,他得變成一些動物(蒼蠅或蚊子)去通知他師傅啊,通知唐僧悟空來救他了,這裡通知就是Notification,那唐僧知道了,他就放心了,安心等著悟空來救.呵呵,讓我看一下main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><Button android:text="通知師傅悟空來救他了" android:id="@+id/wukong"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><Button android:text="通知師傅八戒來救他了" android:id="@+id/bajie"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><Button android:text="通知師傅沙僧來救他了" android:id="@+id/shaseng"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><Button android:text="取消通知" android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/notify_cancal"/></LinearLayout>

這裡面沒有什麼特別的,只是定義了四個按鈕,然後主Activity.java

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;import android.widget.Button;public class NotificationDemo extends Activity{private final static int NOTIFYCATION_ID = 0x11;@Overrideprotected void onCreate(Bundle savedInstanceState){// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.notification);//尋找main.xml中的Button控制項Button wukong = (Button) findViewById(R.id.wukong);wukong.setOnClickListener(new ClickEvent());Button bajie = (Button) findViewById(R.id.bajie);bajie.setOnClickListener(new ClickEvent());Button shaseng = (Button) findViewById(R.id.shaseng);shaseng.setOnClickListener(new ClickEvent());Button notify_cancal = (Button)findViewById(R.id.notify_cancal);notify_cancal.setOnClickListener(new ClickEvent());}class ClickEvent implements View.OnClickListener{@Overridepublic void onClick(View v){//擷取系統的NotificationManager服務NotificationManager notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//建立一個啟動其他Activity的IntentIntent intent = new Intent(NotificationDemo.this,otherActivity.class);//設定點擊通知時顯示內容啟動的類PendingIntent pi = PendingIntent.getActivity(NotificationDemo.this,0, intent, 0);//建立Notification對象Notification notify = new Notification();//設定Notification的發送時間notify.when = System.currentTimeMillis();//為Notification設定預設的聲音,預設的震動,預設的閃光燈,但這些需要在AndroidManifest.xml中加入相應的許可權,不然會報錯notify.defaults = Notification.DEFAULT_ALL;switch (v.getId()){case R.id.wukong://設定Notification的表徵圖notify.icon = R.drawable.wukong;//設定Notification事件資訊notify.setLatestEventInfo(NotificationDemo.this, "悟空","悟空來救師傅的通知", pi);//設定Notification的常值內容,會顯示在狀態列中notify.tickerText = "悟空來救師傅了";//發送通知notifyManager.notify(NOTIFYCATION_ID, notify);break;case R.id.bajie://同上notify.icon = R.drawable.bajie;notify.setLatestEventInfo(NotificationDemo.this, "八戒","八戒來救師傅的通知", pi);notify.tickerText = "八戒來救師傅了";notifyManager.notify(NOTIFYCATION_ID, notify);break;case R.id.shaseng://同上notify.icon = R.drawable.shaseng;notify.setLatestEventInfo(NotificationDemo.this, "沙僧","沙僧來救師傅的通知", pi);notify.tickerText = "沙僧來救師傅了";notifyManager.notify(NOTIFYCATION_ID, notify);break;case R.id.notify_cancal://取消通知notifyManager.cancel(NOTIFYCATION_ID);}}}}

OK,我們在AndroidManifest.xml中加入相應的許可權和要啟動的Activity

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.kang.button_demo"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="10" />    <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission>    <uses-permission android:name="android.permission.VIBRATE"></uses-permission>    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:label="@string/app_name" android:name=".NotificationDemo">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>                <activity android:name=".otherActivity"></activity>    </application></manifest>

好了,都寫完了,運行一下呢,:

這裡的要點就是Notification和NotificationManager的建立,其實掌握Notification對於Android應用開發還是很有用的,好了,這一章就結束了,謝謝

聯繫我們

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