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

首頁 > 系統 > 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);
  }
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 自贡市| 高平市| 连南| 兰州市| 绥宁县| 丹巴县| 银川市| 浦江县| 鄱阳县| 江安县| 彝良县| 望都县| 灵山县| 江北区| 白水县| 龙游县| 谢通门县| 子长县| 利辛县| 通榆县| 琼结县| 正蓝旗| 镇巴县| 庆阳市| 两当县| 威远县| 大荔县| 贵德县| 梧州市| 筠连县| 望奎县| 澎湖县| 长武县| 临邑县| 牡丹江市| 崇礼县| 兰西县| 宜黄县| 新蔡县| 根河市| 富裕县|