最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

Android狀態(tài)欄提醒

在Android中提醒功能也可以用AlertDialog,但是我們要慎重的使用,因?yàn)楫?dāng)使用AlertDialog的時候,用戶正在進(jìn)行的操作將會被打斷,因?yàn)楫?dāng)前焦點(diǎn)被AlertDialog得到。我們可以想像一下,當(dāng)用戶打游戲正爽的時候,這時候來了一條短信。如果這時候短信用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;// 添加震動
  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)文章

最新評論

故城县| 揭东县| 苏尼特右旗| 内丘县| 宝鸡市| 吉林市| 兴国县| 吴桥县| 肇源县| 论坛| 西盟| 永清县| 阳曲县| 瓮安县| 吉隆县| 兴安县| 东平县| 麻阳| 班戈县| 宜君县| 宁强县| 弥勒县| 前郭尔| 陕西省| 尼木县| 阳江市| 黄骅市| 乐亭县| 奉节县| 和静县| 浦县| 岚皋县| 太仆寺旗| 阳曲县| 无为县| 平谷区| 桃源县| 迁西县| 繁昌县| 乐至县| 清苑县|