Android編程自定義Notification實例分析
本文實例講述了Android編程自定義Notification的用法。分享給大家供大家參考,具體如下:
Notification是一種讓你的應用程序在不使用Activity的情況下警示用戶,Notification是看不見的程序組件警示用戶有需要注意的事件發(fā)生的最好途徑。
作為UI部分,Notification對移動設備來說是最適合不過的了。用戶可能隨時都帶著手機在身邊。一般來說,用戶會在后臺打開幾個程序,但不會注意它們。在這樣的情形下,當發(fā)生需要注意的事件時,能夠通知用戶是很重要的。
Notification由NotificationManger統(tǒng)一管理,目前包含的能力有:
❑創(chuàng)建一個狀態(tài)條圖標。
❑在擴展的狀態(tài)條窗口中顯示額外的信息(和啟動一個Intent)。
❑閃燈或LED。
❑電話震動。
❑發(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 ){
//實例化一個notification
String tickerText = "IP號碼 設置完畢";
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.icon, tickerText, when);
//不能手動清理
//notification.flags= Notification.FLAG_NO_CLEAR;
//添加音樂
//notification.sound = Uri.parse("/sdcard/haha.mp3");
//設置用戶點擊notification的動作
// 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程序設計有所幫助。
- android notification 的總結分析
- Android中通知Notification使用實例(振動、燈光、聲音)
- android中創(chuàng)建通知欄Notification代碼實例
- Android中關于Notification及NotificationManger的詳解
- Android編程實現攔截短信并屏蔽系統(tǒng)Notification的方法
- Android中通過Notification&NotificationManager實現消息通知
- 詳解Android中Notification通知提醒
- Android中Notification用法實例總結
- Android開發(fā) -- 狀態(tài)欄通知Notification、NotificationManager詳解
- Android中Notification的用法匯總
- Android編程開發(fā)之NotiFication用法詳解
- Android開發(fā)入門之Notification用法分析
相關文章
Android Studio 4.0 新功能中的Live Layout Inspector詳解
這篇文章主要介紹了Android Studio 4.0 新功能中的Live Layout Inspector,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
Android項目實戰(zhàn)之仿網易新聞的頁面(RecyclerView )
這篇文章主要介紹了Android項目實戰(zhàn)之仿網易新聞的頁面,ViewPager作為RecyclerView的Header,感興趣的小伙伴們可以參考一下2016-01-01
android工程下不能運行java main程序的解決方法
這篇文章主要介紹了android工程下不能運行java main程序的解決方法,需要的朋友可以參考下2014-05-05
AndroidApk混淆編譯時,報告java.io.IOException...錯誤解決辦法
這篇文章主要介紹了 AndroidApk混淆編譯時,報告Error:Execution failed for task ‘:gviews:transformClassesAndResourcesWithProguardForRelease’.錯誤解決辦法的相關資料,需要的朋友可以參考下2017-03-03
android 如何設置開機后屏幕亮度默認值為自動調節(jié)
在第一次開機后,設置>顯示>自動亮度調節(jié) 默認是勾選上的,具體修改方法如下,感興趣的朋友可以嘗試操作下2013-06-06

