Android編程自定義Notification實(shí)例分析
本文實(shí)例講述了Android編程自定義Notification的用法。分享給大家供大家參考,具體如下:
Notification是一種讓你的應(yīng)用程序在不使用Activity的情況下警示用戶,Notification是看不見的程序組件警示用戶有需要注意的事件發(fā)生的最好途徑。
作為UI部分,Notification對移動(dòng)設(shè)備來說是最適合不過的了。用戶可能隨時(shí)都帶著手機(jī)在身邊。一般來說,用戶會(huì)在后臺打開幾個(gè)程序,但不會(huì)注意它們。在這樣的情形下,當(dāng)發(fā)生需要注意的事件時(shí),能夠通知用戶是很重要的。
Notification由NotificationManger統(tǒng)一管理,目前包含的能力有:
❑創(chuàng)建一個(gè)狀態(tài)條圖標(biāo)。
❑在擴(kuò)展的狀態(tài)條窗口中顯示額外的信息(和啟動(dòng)一個(gè)Intent)。
❑閃燈或LED。
❑電話震動(dòng)。
❑發(fā)出聽得見的警告聲(鈴聲,保存的聲音文件)。
自定義Notification效果圖:

自定義的布局文件:
<?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"> <TextView android:id="@+id/tv_rv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="haha" /> <ProgressBar style="@android:style/Widget.ProgressBar.Horizontal" android:id="@+id/pb_rv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
創(chuàng)建Notification:
public class CustomNotificationActivity extends Activity {
NotificationManager notificationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//獲取到系統(tǒng)的notificationManager
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
public void click(View view ){
//實(shí)例化一個(gè)notification
String tickerText = "IP號碼 設(shè)置完畢";
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.icon, tickerText, when);
//不能手動(dòng)清理
//notification.flags= Notification.FLAG_NO_CLEAR;
//添加音樂
//notification.sound = Uri.parse("/sdcard/haha.mp3");
//設(shè)置用戶點(diǎn)擊notification的動(dòng)作
// pendingIntent 延期的意圖
Intent intent = new Intent(this,Bactivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.contentIntent = pendingIntent;
//自定義界面
RemoteViews rv = new RemoteViews(getPackageName(), R.layout.noti_layout);
rv.setTextViewText(R.id.tv_rv, "我是自定義的 notification");
rv.setProgressBar(R.id.pb_rv, 80, 20, false);
notification.contentView = rv;
//把定義的notification 傳遞給 notificationmanager
notificationManager.notify(0, notification);
}
}
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- android notification 的總結(jié)分析
- Android中通知Notification使用實(shí)例(振動(dòng)、燈光、聲音)
- android中創(chuàng)建通知欄Notification代碼實(shí)例
- Android中關(guān)于Notification及NotificationManger的詳解
- Android編程實(shí)現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法
- Android中通過Notification&NotificationManager實(shí)現(xiàn)消息通知
- 詳解Android中Notification通知提醒
- Android中Notification用法實(shí)例總結(jié)
- Android開發(fā) -- 狀態(tài)欄通知Notification、NotificationManager詳解
- Android中Notification的用法匯總
- Android編程開發(fā)之NotiFication用法詳解
- Android開發(fā)入門之Notification用法分析
相關(guān)文章
Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能示例
這篇文章主要介紹了Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能,結(jié)合實(shí)例形式分析了widget組件在桌面應(yīng)用中的事件響應(yīng)與屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
Android開發(fā)中Eclipse報(bào)錯(cuò)及對應(yīng)處理方法總結(jié)
這篇文章主要介紹了Android開發(fā)中Eclipse報(bào)錯(cuò)及對應(yīng)處理方法,實(shí)例匯總了使用eclipse開發(fā)Android項(xiàng)目過程中常見的錯(cuò)誤提示及對應(yīng)的處理技巧,需要的朋友可以參考下2015-12-12
android4.0屏蔽HOME鍵的簡單實(shí)現(xiàn)
這篇文章主要介紹了android4.0屏蔽HOME鍵的簡單實(shí)現(xiàn),需要的朋友可以參考下2014-02-02
Flutter實(shí)現(xiàn)單選,復(fù)選和開關(guān)組件的示例代碼
在App開發(fā)過程中,選擇交互是非常常見的,今天主要介紹下關(guān)于選擇的三個(gè)組件的使用:開關(guān)、單選和復(fù)選,感興趣的小伙伴可以了解一下2022-04-04
Android入門教程之Vibrator(振動(dòng)器)
本節(jié)我們介紹的是Vibrator(振動(dòng)器),是手機(jī)自帶的振動(dòng)器,其實(shí)就是Android給我們提供的用于機(jī)身震動(dòng)的一個(gè)服務(wù)!當(dāng)收到推送消息的時(shí)候我們可以設(shè)置震動(dòng)提醒。2016-07-07
android 6.0下webview的定位權(quán)限設(shè)置方法
今天小編就為大家分享一篇android 6.0下webview的定位權(quán)限設(shè)置方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

