android系统 通知管理,Android的通知系统
Android的通知系統
默認分類 | 2015-07-07 08:21:24 | 閱讀 1581 次 | 評論(0)
:
將應用程序的一些重要信息通知給用戶。
1、Toast
形式:一般在界面下半部分,彈出一個黑色的方框,不會影響用戶操作,過一陣子自已會消失(不會因為界面的消失而消失)
特性:1、Toast提示消息不會獲取焦點
2、Toast提示信息過一段時間就會自動消失,不需要用戶確認(反饋)
步驟:
1、通過Toast的靜態方法:makeText(context,文本,Toast顯示的時間)創建一個Toast對象。
makeText()參數:1、上下文對象context,直接指向activity本身
2、文本:消息內容
3、Toast顯示的時間:
Toast.LENGTH_LONG 長
Toast.LENGTH_SHORT 短
2、對象.show() 將toast展示出去。
例:Toast.show()
應用場景:提示用戶當前狀態。
信息量少的提示。
不需要用戶確認反饋的信息。
2、Notification
是顯示在手機狀態欄上面的通知。它代表的是一種全局效果的通知。
作用:來消息的時候顯示在通知欄上面,當用戶點擊的時候就會跳轉到詳細頁面,
使用場景:時效性不是很強的信息。
例如:短信,推送信息
步驟:
1、//得到一個消息管理器 (設置成成員變量)
mManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
3、//創建notification對象
Notification notification = new Notification(R.drawable.ic_launcher, "消息來了!",
System.currentTimeMillis());
6、//設置意圖對象
Intent intent = new Intent();
intent.setClass(this, SecondActivity.class);
5、//設置關聯的activity
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, intent ,0);
4、//設置消息主體內容
notification.setLatestEventInfo(this,"title","message~~",
contentIntent );
7、//設置notification的標志位
//點擊后自動消失
//notification.flags = Notification.FLAG_AUTO_CANCEL;
//消息處于運行欄
notification.flags = Notification.FLAG_ONGOING_EVENT;
2、//通過消息管理器發送消息
mManager.notify(123, notification );
//取消通知
mManager.cancel(123);//參數:發送時候的id
注意:android4.1 創建notification對象有個新的方法:
Notification.Builder(this)
3、Dialog 對話框
通常是出現在activity前面的一個小窗體。
對話框通常用戶程序短暫中斷是提示用戶信息。
AlertDialog:
創建方式一:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//設置標題
builder.setTitle("對話框");
//設置對話框內容
builder.setMessage("對話框內容");
//設置對話框圖標
builder.setIcon(R.drawable.ic_launcher);
//設置按鈕 共三個
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
builder.setNegativeButton("取消", null);
builder.setNeutralButton("應用", null);
//創建對話框
AlertDialog dialog = builder.create();
//顯示對話框
dialog.show();
創建方式二:
1、調用showDialog(id)
2、重寫onCreateDialog()
返回一個AlertDialog對象
菜單 Menu
選項菜單:OptionMenu
創建方式:
1、創建:
重寫onCreateOptionsMenu()
menu.add(0, 1, 0, "設置").setIcon(R.drawable.ic_launcher);
子菜單:
SubMenu subMenu = menu.addSubMenu(0, 9, 0, "子菜單");
subMenu.add(0, 10, 0, "子菜單1");
2、點擊事件:
重寫onOptionsItemSelected()
switch (item.getItemId()) {
case 10:
Toast.makeText(this, "設置", Toast.LENGTH_LONG).show();
break;
default:
break;
}
第二種:xml
作業:1、練習通知系統、菜單(選項菜單)
文章評論,共0條
游客請輸入驗證碼
總結
以上是生活随笔為你收集整理的android系统 通知管理,Android的通知系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android读写文本文件,Androi
- 下一篇: 深度操作系统 deepin for RI