欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中關(guān)于Notification及NotificationManger的詳解

 更新時(shí)間:2013年04月21日 09:52:23   作者:  
本篇文章小編為大家介紹,Android中關(guān)于Notification及NotificationManger的詳解。需要的朋友參考下

Android狀態(tài)欄提醒

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

步驟:

一、添加布局對象

復(fù)制代碼 代碼如下:

<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并實(shí)現(xiàn)接口OnClickListener
復(fù)制代碼 代碼如下:

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;// 使用默認(rèn)的提示聲音
  noti.defaults |= Notification.DEFAULT_VIBRATE;// 添加震動(dòng)
  notiManger = (NotificationManager) this.getSystemService(mContext.NOTIFICATION_SERVICE);//獲取NofificationManger對象
  showbtn.setOnClickListener(this);//讓Activity實(shí)現(xiàn)接口OnClickListener可以簡單的通過此兩行代碼添加按鈕點(diǎn)擊響應(yīng)事件
  calclebtn.setOnClickListener(this);
 }

 // 按鈕點(diǎn)擊事件響應(yīng)
 @Override
 public void onClick(View v) {
  if (v == showbtn) {
   Intent intent = new Intent(this.getApplicationContext(),this.getClass());
   // 設(shè)置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)設(shè)置(上下文,標(biāo)題,內(nèi)容,PendingInteng)
   noti.setLatestEventInfo(this, "10086", "你從此以后免除所有話費(fèi)", contentIntent);
   // 發(fā)送通知(消息ID,通知對象)
   notiManger.notify(NOTIFICATION_ID, noti);
  } else if (v == calclebtn) {
   // 取消通知(id)
   notiManger.cancel(NOTIFICATION_ID);
  }
 }
}

相關(guān)文章

  • Android Handler中的休眠喚醒實(shí)現(xiàn)詳解

    Android Handler中的休眠喚醒實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了Android Handler中的休眠喚醒實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android實(shí)現(xiàn)點(diǎn)擊兩次返回鍵退出

    Android實(shí)現(xiàn)點(diǎn)擊兩次返回鍵退出

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊兩次返回鍵退出的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式

    Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式

    本篇文章主要介紹了Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-09-09
  • Flutter 路由插件fluro的使用

    Flutter 路由插件fluro的使用

    使用原生的路由基本上能夠滿足大部分需求,但如果想要對頁面做類似瀏覽器 url 那樣的路由,或者控制頁面跳轉(zhuǎn)的轉(zhuǎn)場動(dòng)畫,那么原生的路由需要做不少的改造。在 pub 上,有優(yōu)秀的路由插件 fluro 解決這類問題。本文介紹該插件的使用方法
    2021-06-06
  • 詳解Android 消息處理機(jī)制

    詳解Android 消息處理機(jī)制

    這篇文章主要介紹了Android 消息處理機(jī)制的相關(guān)資料,幫助大家更好的進(jìn)行Android開發(fā),感興趣的朋友可以了解下
    2020-10-10
  • 完美解決虛擬按鍵遮蓋底部視圖的問題

    完美解決虛擬按鍵遮蓋底部視圖的問題

    下面小編就為大家分享一篇完美解決虛擬按鍵遮蓋底部視圖的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Android高性能日志寫入方案的實(shí)現(xiàn)

    Android高性能日志寫入方案的實(shí)現(xiàn)

    這篇文章主要給大家介紹了關(guān)于Android高性能日志寫入方案的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • Android小程序?qū)崿F(xiàn)選項(xiàng)菜單

    Android小程序?qū)崿F(xiàn)選項(xiàng)菜單

    這篇文章主要為大家詳細(xì)介紹了Android小程序?qū)崿F(xiàn)選項(xiàng)菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • android studio 新手入門教程(三)Github( ignore忽略規(guī)則)的使用教程圖解

    android studio 新手入門教程(三)Github( ignore忽略規(guī)則)的使用教程圖解

    這篇文章主要介紹了android studio 新手入門教程(三)Github( ignore忽略規(guī)則)的使用教程圖解,需要的朋友可以參考下
    2017-12-12
  • Kotlin fun函數(shù)使用方法

    Kotlin fun函數(shù)使用方法

    函數(shù)是執(zhí)行特定任務(wù)的一組相互關(guān)聯(lián)的代碼塊。函數(shù)用于將程序分解為不同的子模塊。它使代碼可重用,并使程序更易于管理,這篇文章主要介紹了Kotlin fun函數(shù)使用方法
    2022-12-12

最新評論