詳解Android中Notification的使用方法
在消息通知的時候,我們經(jīng)常用到兩個控件Notification和Toast。特別是重要的和需要長時間顯示的信息,用Notification最合適不過了。他可以在頂部顯示一個圖標以標示有了新的通知,當我們拉下通知欄的時候,可以看到詳細的通知內(nèi)容。
最典型的應用就是未看短信和未接來電的顯示,還有QQ微信,我們一看就知道有一個未接來電或者未看短信,收到QQ離線信息。同樣,我們也可以自定義一個Notification來定義我們自己的程序想要傳達的信息。
Notification我把他分為兩種,一種是默認的顯示方式,另一種是自定義的,今天為大家講述默認的顯示方式:
1、程序框架結(jié)構(gòu)圖如下

2、布局文件 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="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#EEE" android:textStyle="bold" android:textSize="25sp" android:text="NotificationDemo實例" /> <Button android:id="@+id/btnSend" android:text="send notification" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/> </LinearLayout>
3、MainActivity.java源碼如下:
package com.andyidea.notification;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btnSend;
//定義BroadcastReceiver的action
private static final String NotificationDemo_Action = "com.andyidea.notification.NotificationDemo_Action";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSend = (Button)findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(NotificationDemo_Action);
sendBroadcast(intent);
}
});
}
}
4、布局文件 secondlayou.xml 源碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#EEE" android:textStyle="bold" android:textSize="25sp" android:text="顯示通知界面" /> <Button android:id="@+id/btnCancel" android:text="cancel notification" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout>
5、SecondActivity.java源碼如下:
package com.andyidea.notification;
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 SecondActivity extends Activity {
private Button btnCancel;
//聲明Notification
private Notification notification;
//聲明NotificationManager
private NotificationManager mNotification;
//標識Notification的ID
private static final int ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondlayout);
btnCancel = (Button)findViewById(R.id.btnCancel);
//怎樣獲得NotificationManager的實例?
String service = NOTIFICATION_SERVICE;
mNotification = (NotificationManager)getSystemService(service);
//獲得Notification的實例
notification = new Notification();
//設置該圖標 會在狀態(tài)欄顯示
int icon = notification.icon = android.R.drawable.stat_sys_phone_call;
//設置提示信息
String tickerText = "Test Notification";
//設置顯示時間
long when = System.currentTimeMillis();
notification.icon = icon;
notification.tickerText = tickerText;
notification.when = when;
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "消息", "SMS Android", pi);
mNotification.notify(ID, notification);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mNotification.cancel(ID); //--->取消通知
}
});
}
}
6、NotificationReceiver.java源碼如下:
package com.andyidea.notification;
import com.andyidea.notification.SecondActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//實例化Intent
Intent i = new Intent();
//在新任務中啟動Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//設置Intent啟動的組件名稱
i.setClass(context, SecondActivity.class);
//啟動Activity,顯示通知
context.startActivity(i);
}
}
7、程序運行效果如下:

以上就是針對Android中Notification使用方法進行的詳細介紹,希望對大家的學習有所啟發(fā),幫助大家更好地學習Android軟件編程。
- android notification 的總結(jié)分析
- Android界面 NotificationManager使用Bitmap做圖標
- Android中通知Notification使用實例(振動、燈光、聲音)
- Android中通過Notification&NotificationManager實現(xiàn)消息通知
- android中創(chuàng)建通知欄Notification代碼實例
- Android編程實現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法
- Android開發(fā) -- 狀態(tài)欄通知Notification、NotificationManager詳解
- Android中關(guān)于Notification及NotificationManger的詳解
- 詳解Android中Notification通知提醒
- Android中Notification 提示對話框
- Android中Notification用法實例總結(jié)
- android 通知Notification詳解及實例代碼
相關(guān)文章
Android中Market的Loading效果實現(xiàn)方法
這篇文章主要介紹了Android中Market的Loading效果實現(xiàn)方法,較為詳細的分析了Android中l(wèi)oading效果的相關(guān)布局及功能實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android 自定義彈性ListView控件實例代碼(三種方法)
關(guān)于在Android中實現(xiàn)ListView的彈性效果,有很多不同的方法,網(wǎng)上一搜,也有很多,下面貼出在項目中經(jīng)常用到的兩種實現(xiàn)ListView彈性效果的方法(基本上拿來就可以用),需要的朋友參考下本段代碼2016-01-01
Android中的webview監(jiān)聽每次URL變化實例
這篇文章主要介紹了Android中的webview監(jiān)聽每次URL變化實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android在類微信程序中實現(xiàn)藍牙聊天功能的示例代碼
這篇文章主要介紹了Android在類微信程序中實現(xiàn)藍牙聊天功能,本文通過實例代碼給大家介紹的非常想詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06

