国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程自定義Notification實例分析

2020-04-11 11:05:30
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程自定義Notification的用法。分享給大家供大家參考,具體如下:

Notification是一種讓你的應用程序在不使用Activity的情況下警示用戶,Notification是看不見的程序組件警示用戶有需要注意的事件發生的最好途徑。

作為UI部分,Notification對移動設備來說是最適合不過的了。用戶可能隨時都帶著手機在身邊。一般來說,用戶會在后臺打開幾個程序,但不會注意它們。在這樣的情形下,當發生需要注意的事件時,能夠通知用戶是很重要的。

Notification由NotificationManger統一管理,目前包含的能力有:

❑創建一個狀態條圖標。

❑在擴展的狀態條窗口中顯示額外的信息(和啟動一個Intent)。

❑閃燈或LED。

❑電話震動。

❑發出聽得見的警告聲(鈴聲,保存的聲音文件)。

自定義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>

創建Notification:

public class CustomNotificationActivity extends Activity {  NotificationManager notificationManager;  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    //獲取到系統的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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洞头县| 隆子县| 无棣县| 如东县| 鱼台县| 武平县| 城固县| 马公市| 佛教| 太仓市| 和政县| 大理市| 黄浦区| 尤溪县| 邵阳市| 葫芦岛市| 合江县| 嘉峪关市| 东安县| 绥芬河市| 任丘市| 廉江市| 宁陕县| 大城县| 沽源县| 五寨县| 通州区| 兰考县| 霍州市| 澎湖县| 紫云| 屏东县| 屏边| 杭州市| 景东| 台南县| 兰州市| 阳新县| 温宿县| 吴江市| 伊通|