a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android中關于Notification及NotificationManger的詳解

2020-04-11 12:28:52
字體:
來源:轉載
供稿:網友

Android狀態欄提醒

在Android中提醒功能也可以用AlertDialog,但是我們要慎重的使用,因為當使用AlertDialog的時候,用戶正在進行的操作將會被打斷,因為當前焦點被AlertDialog得到。我們可以想像一下,當用戶打游戲正爽的時候,這時候來了一條短信。如果這時候短信用AlertDialog提醒,用戶必須先去處理這條提醒,從而才能繼續游戲。用戶可能會活活被氣死。而使用Notification就不會帶來這些麻煩事,用戶完全可以打完游戲再去看這條短信。所以在開發中應根據實際需求,選擇合適的控件。

步驟:

一、添加布局對象

復制代碼 代碼如下:

<Button
android:id="@+id/showButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="showNotification" />

<Button
android:id="@+id/cancelButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="cancelNotification" />


二、修改MianActivity繼承處Activity并實現接口OnClickListener
復制代碼 代碼如下:

public class MainActivity extends Activity implements OnClickListener {
 private Context mContext = this;
 private Button showbtn, calclebtn;
 private Notification noti;
 private NotificationManager notiManger;
 private static int NOTIFICATION_ID = 0x0001;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  setUpViews();
 }

 private void setUpViews() {
  showbtn = (Button) findViewById(R.id.showButton);
  calclebtn = (Button) findViewById(R.id.cancelButton);
  noti = new Notification(R.drawable.ic_launcher, "this is a notification", System.currentTimeMillis());
  noti.defaults = Notification.DEFAULT_SOUND;// 使用默認的提示聲音
  noti.defaults |= Notification.DEFAULT_VIBRATE;// 添加震動
  notiManger = (NotificationManager) this.getSystemService(mContext.NOTIFICATION_SERVICE);//獲取NofificationManger對象
  showbtn.setOnClickListener(this);//讓Activity實現接口OnClickListener可以簡單的通過此兩行代碼添加按鈕點擊響應事件
  calclebtn.setOnClickListener(this);
 }

 // 按鈕點擊事件響應
 @Override
 public void onClick(View v) {
  if (v == showbtn) {
   Intent intent = new Intent(this.getApplicationContext(),this.getClass());
   // 設置Intent.FLAG_ACTIVITY_NEW_TASK
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
   // noti.setLatestEventInfo(context, contentTitle, contentText, contentIntent)設置(上下文,標題,內容,PendingInteng)
   noti.setLatestEventInfo(this, "10086", "你從此以后免除所有話費", contentIntent);
   // 發送通知(消息ID,通知對象)
   notiManger.notify(NOTIFICATION_ID, noti);
  } else if (v == calclebtn) {
   // 取消通知(id)
   notiManger.cancel(NOTIFICATION_ID);
  }
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 自拍偷拍第一页 | 美女主播精品视频一二三四 | av直接看| 欧美黑人xxx| 国产精品久久久久久久久久东京 | 男女羞羞视频免费在线观看 | 伊人青青久久 | 97久久久国产精品 | 国产欧美日韩精品一区 | 天天干 夜夜操 | 国产一区不卡 | 成人av网站在线观看 | 日韩在线影院 | 91精品在线观看入口 | 免费的色网站 | 日韩精品在线观看视频 | 色呦呦网 | 欧美一区2区三区4区公司二百 | 天天做天天爱天天综合网2021 | 欧美三区 | 亚洲欧美视频 | 美女主播精品视频一二三四 | 成人一区二区三区在线 | 亚洲www| 亚洲蜜臀av乱码久久精品蜜桃 | 成人a在线视频免费观看 | 91精品久久久久久 | 久久一 | 黄色毛片在线观看 | 香蕉二区| 午夜91| 色播开心网 | hd国产人妖ts另类视频 | 免费观看h视频 | 久草精品在线观看 | 嫩草久久| 午夜视频 | 欧美日韩视频在线 | 久久精品国产一区二区电影 | 超碰导航| 91免费在线看 |