亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開(kāi)發(fā)之Notification手機(jī)狀態(tài)欄通知用法實(shí)例分析

 更新時(shí)間:2019年03月16日 09:38:43   作者:水中魚(yú)之1999  
這篇文章主要介紹了Android開(kāi)發(fā)之Notification手機(jī)狀態(tài)欄通知用法,結(jié)合實(shí)例形式分析了Android Notification手機(jī)狀態(tài)欄通知的常見(jiàn)函數(shù)、功能及使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Android開(kāi)發(fā)之Notification手機(jī)狀態(tài)欄通知用法。分享給大家供大家參考,具體如下:

簡(jiǎn)介:

通知是顯示在手機(jī)狀態(tài)欄的通知(PS:就是手機(jī)上方,顯示時(shí)間啥的那一欄)

用法:

Notification添加了Builder()類(lèi),其包含如下方法:

1. setDefaults()         通知led燈、音樂(lè)、震動(dòng)等

2. setAutoChange()  設(shè)置點(diǎn)擊通知后,通知自動(dòng)從狀態(tài)欄刪除

3. setContentTitle()   通知標(biāo)題

4. setContentText()  通知內(nèi)容

5. setSmallcon()      為通知設(shè)置圖標(biāo)

6. setLargelcon()       為通知設(shè)置大圖標(biāo)

7. setTick()               設(shè)置通知狀態(tài)欄的提示文本

8. setContentIntent()點(diǎn)擊通知后要啟動(dòng)的相應(yīng)組件

運(yùn)行效果:

實(shí)現(xiàn)方法:

1.首先建立一個(gè)活動(dòng)用來(lái)執(zhí)行:

public class MainActivity extends Activity {
  static final int NOTIFICATION_ID = 0x123;
  NotificationManager notificationManager;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //獲取系統(tǒng)的Notification對(duì)象
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
   }
  //為發(fā)送通知的按鈕點(diǎn)擊事件定義事件處理方法
  @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
  public void send(View source){
    //創(chuàng)建一個(gè)其他Activity的Intent
    Intent intent = new Intent(MainActivity.this,TextActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
    Notification notification = new Notification.Builder(this)
        //設(shè)置打開(kāi)通知 通知自動(dòng)消失
        .setAutoCancel(true)
        //設(shè)置顯示狀態(tài)欄的通知提示信息
        .setTicker("注目提醒!")
        //設(shè)置通知圖標(biāo)
        .setSmallIcon(R.drawable.seek02)
        //設(shè)置通知內(nèi)容標(biāo)題
        .setContentTitle("該應(yīng)用發(fā)生 爆炸大 大 大 新聞??!")
        //設(shè)置通知內(nèi)容
        .setContentText("冒險(xiǎn)沒(méi)有 你手機(jī)自嗨罷了~")
        //設(shè)置使用默認(rèn)的聲音 LED燈
        .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS)
        //設(shè)置通知自定義聲音
//        .setSound()
        .setWhen(System.currentTimeMillis())
        //設(shè)置他只要啟動(dòng)的程序Intent
        .setContentIntent(pendingIntent)
        .build();
    notificationManager.notify(NOTIFICATION_ID,notification);
  }
  public void del(View view){
    //取消通知
    notificationManager.cancel(NOTIFICATION_ID);
  }
}

2.然后建立一個(gè)要打開(kāi)的活動(dòng)(隨意建就行)(布局文件任意我這里就不寫(xiě)了)

public class TextActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_t_exta_ctivity);
  }
}

最后記得添加權(quán)限(mainfest)

<!--消息通知使用到閃光燈和聲音權(quán)限-->
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.VIBRATE"/>

PS:關(guān)于Android權(quán)限控制可參考~
Android Manifest功能與權(quán)限描述大全: http://tools.jb51.net/table/AndroidManifest

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論