安卓開發_淺談Notification(通知欄)

來源:互聯網
上載者:User

標籤:

Notification通知欄
是顯示在手機狀態的訊息,代表一種全域效果的通知

 

快速建立一個Notification的步驟簡單可以分為以下四步:

      第一步:通過getSystemService()方法得到NotificationManager對象;

      第二步:對Notification的一些屬性進行設定比如:內容,表徵圖,標題,相應notification的動作進行處理等等;

      第三步:通過NotificationManager對象的notify()方法來執行一個notification的快訊;

      第四步:通過NotificationManager對象的cancel()方法來取消一個notificatioin的快訊;

 

樣本:

布局:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6  7     <Button 8         android:id="@+id/notification_open" 9         android:layout_width="fill_parent"10         android:layout_height="wrap_content"11         android:text="開啟通知欄" />12 13     <Button14         android:id="@+id/notification_close"15         android:layout_width="fill_parent"16         android:layout_height="wrap_content"17         android:text="取消通知欄" />18 19 </LinearLayout>
布局

 

JAVA檔案:

 1 package information; 2  3  4  5 import android.annotation.SuppressLint; 6 import android.app.Activity; 7 import android.app.Notification; 8 import android.app.Notification.Builder; 9 import android.app.NotificationManager;10 import android.app.PendingIntent;11 import android.content.Context;12 import android.content.Intent;13 import android.os.Bundle;14 import android.view.View;15 import android.view.View.OnClickListener;16 import android.widget.Button;17 18 import com.example.allcode.R;19 20 public class Notification_text_one extends Activity implements OnClickListener{21     NotificationManager manger; //通知控制類22     int notification_id;23     private Button open;24     private Button close;25     @Override26     protected void onCreate(Bundle savedInstanceState) {27         // TODO Auto-generated method stub28         super.onCreate(savedInstanceState);29         setContentView(R.layout.notification);30         31         open = (Button) findViewById(R.id.notification_open);32         close = (Button) findViewById(R.id.notification_close);33         //系統服務34         manger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);35         36         open.setOnClickListener(this);37         close.setOnClickListener(this);38     }39     @Override40     public void onClick(View v) {41         // TODO Auto-generated method stub42         switch (v.getId()) {43         case R.id.notification_open:  //開啟通知欄44             sendNotification();45             46             break;47         case R.id.notification_close:48             manger.cancel(notification_id);  //取消通知欄49             50             break;51 52         default:53             break;54         }55     }56     57     private void sendNotification(){58         Intent intent = new Intent(this,AlertDialog_text.class);59         60         PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);61         Builder builder = new Notification.Builder(this);62         builder.setSmallIcon(R.drawable.icon_72);  //設定通知欄表徵圖63         builder.setTicker("Hello");  //設定通知欄提示64         builder.setWhen(System.currentTimeMillis());//設定時間65         builder.setContentTitle("這是通知欄標題");//通知欄標題66         builder.setContentText("這裡是通知欄內容");//通知欄內容67         builder.setContentIntent(pi);//設定點擊後的意圖68         //效果,需要添加相應的許可權69         builder.setDefaults(Notification.DEFAULT_SOUND);//設定提示聲音70         builder.setDefaults(Notification.DEFAULT_LIGHTS);//設定指示燈71         builder.setDefaults(Notification.DEFAULT_VIBRATE);//設定震動72         //builder.setDefaults(Notification.DEFAULT_ALL);//設定全部效果
      //許可權 <uses-permission android:name="android.permission.VIBRATE" />73 //Notification notification = builder.build();//安卓版本4.1及以上74 Notification notification = builder.getNotification();//安卓版本4.1以下75 manger.notify(notification_id,notification);76 }77 }


PendingIntent.getActivity(this, 0, intent, 0);
參數:
第二個:
    id
第四個:

  設定flag位

       FLAG_AUTO_CANCEL        該通知能被狀態列的清除按鈕給清除掉

       FLAG_NO_CLEAR           該通知能被狀態列的清除按鈕給清除掉

       FLAG_ONGOING_EVENT      通知放置在正在運行

       FLAG_INSISTENT          是否一直進行,比如音樂一直播放,知道使用者響應



1 notification.flags = Notification.FLAG_NO_CLEAR; // 點擊清除按鈕時就會清除訊息通知,但是點擊通知欄的通知時不會消失 2 notification.flags = Notification.FLAG_ONGOING_EVENT; // 點擊清除按鈕不會清除訊息通知,可以用來表示在正在運行 3 notification.flags |= Notification.FLAG_AUTO_CANCEL; // 點擊清除按鈕或點擊通知後會自動消失 4 notification.flags |= Notification.FLAG_INSISTENT; // 一直進行,比如音樂一直播放,知道使用者響應

安卓開發_淺談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.