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

首頁(yè) > 系統(tǒng) > Android > 正文

Notification及NotificationManger的詳細(xì)分析

2020-02-21 17:37:23
字體:
供稿:網(wǎng)友

  今天小編跟大家分享一篇關(guān)于Notification及NotificationManger的詳細(xì)分析,感興趣的朋友跟小編一起來了解一下吧!

  Android狀態(tài)欄提醒

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

  步驟:

  一、添加布局對(duì)象

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

  

  android:id="@+id/showButton"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text="showNotification" />

  

  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對(duì)象

  showbtn.setOnClickListener(this);//讓Activity實(shí)現(xiàn)接口OnClickListener可以簡(jiǎn)單的通過此兩行代碼添加按鈕點(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,通知對(duì)象)

  notiManger.notify(NOTIFICATION_ID, noti);

  } else if (v == calclebtn) {

  // 取消通知(id)

  notiManger.cancel(NOTIFICATION_ID);

  }

  }

  }

  以上就是Notification及NotificationManger的詳細(xì)分析了,更多相關(guān)內(nèi)容請(qǐng)繼續(xù)關(guān)注武林技術(shù)頻道。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 广德县| 襄汾县| 康马县| 泰兴市| 佛教| 巴林左旗| 娄底市| 利津县| 冕宁县| 齐齐哈尔市| 中江县| 九龙县| 岚皋县| 武安市| 榆社县| 逊克县| 富蕴县| 临漳县| 潮州市| 马龙县| 安图县| 砚山县| 庆元县| 巴彦淖尔市| 潞西市| 定日县| 文山县| 遵义市| 日土县| 广州市| 新余市| 鱼台县| 称多县| 呈贡县| 林周县| 康平县| 韶关市| 石棉县| 新和县| 沽源县| 南京市|