android:Notification實現狀態列的通知

來源:互聯網
上載者:User

標籤:指示   blog   手機螢幕   context   code   效果   state   震動器   efault   

在使用手機時,當有未接來電或者新短訊息時,手機會給出響應的提示資訊,這些提示資訊一般會顯示到手機螢幕的狀態列上。

Android也提供了用於處理這些資訊的類,它們是Notification和NotificationManager。

當中,Notification代表的是具有全域效果的通知,而NotificationManager則是用於發送Notification通知的系統服務。

使用Notification和NotificationManager類發送和顯示通知也比較簡單,大致能夠分為下面四個步驟

(1)調用getSystemService() 方法擷取系統的NotificationManager服務

(2)建立一個Notification對象,並為其設定各種屬性

(3)為Notification對象設定事件資訊

(4)通過NotificationManager類的notify()方法發送Notification通知

以下通過一個執行個體說明和使用Notification在狀態列上顯示通知

國際慣例

執行結果:

布局檔案就不發了 線性垂直布局 兩個button

MainActivity.class

package com.example.notification;import android.os.Bundle;import android.app.Activity;import android.app.Notification;import android.app.Notification.Builder;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener{private NotificationManager manager;private Button button1;private Button button2;private int Notification_ID;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);button1=(Button) findViewById(R.id.button1);button2=(Button) findViewById(R.id.button2);button1.setOnClickListener(this);button2.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.button1:{showNotification();break;}case R.id.button2:{manager.cancel(Notification_ID);break;}}}private void showNotification() {// TODO Auto-generated method stubNotification.Builder builder=new Builder(this);builder.setSmallIcon(R.drawable.ic_launcher);//設定表徵圖builder.setTicker("通知來啦");//手機狀態列的提示builder.setContentTitle("我是通知標題");//設定標題builder.setContentText("我是通知內容");//設定通知內容builder.setWhen(System.currentTimeMillis());//設定通知時間Intent intent=new Intent(this,MainActivity.class);PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, 0);builder.setContentIntent(pendingIntent);//點擊後的意圖builder.setDefaults(Notification.DEFAULT_LIGHTS);//設定指示燈builder.setDefaults(Notification.DEFAULT_SOUND);//設定提示聲音builder.setDefaults(Notification.DEFAULT_VIBRATE);//設定震動Notification notification=builder.build();//4.1以上。下面要用getNotification()manager.notify(Notification_ID, notification);}}

上面代碼中設定的指示燈和震動,因為程式中要訪問系統的指示燈和震動器 所以要在AndroidManifest.xml中聲明使用許可權

    <uses-permission android:name="android.permission.VIBRATE" />    <uses-permission android:name="android.permission.FLASHLIGHT" />

希望對你有所協助。

渣渣學習中....

android: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.