Android開發(fā)之Notification手機狀態(tài)欄通知用法實例分析
本文實例講述了Android開發(fā)之Notification手機狀態(tài)欄通知用法。分享給大家供大家參考,具體如下:
簡介:
通知是顯示在手機狀態(tài)欄的通知(PS:就是手機上方,顯示時間啥的那一欄)
用法:
Notification添加了Builder()類,其包含如下方法:
1. setDefaults() 通知led燈、音樂、震動等
2. setAutoChange() 設(shè)置點擊通知后,通知自動從狀態(tài)欄刪除
3. setContentTitle() 通知標題
4. setContentText() 通知內(nèi)容
5. setSmallcon() 為通知設(shè)置圖標
6. setLargelcon() 為通知設(shè)置大圖標
7. setTick() 設(shè)置通知狀態(tài)欄的提示文本
8. setContentIntent()點擊通知后要啟動的相應組件
運行效果:

實現(xiàn)方法:
1.首先建立一個活動用來執(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對象
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
//為發(fā)送通知的按鈕點擊事件定義事件處理方法
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void send(View source){
//創(chuàng)建一個其他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è)置打開通知 通知自動消失
.setAutoCancel(true)
//設(shè)置顯示狀態(tài)欄的通知提示信息
.setTicker("注目提醒!")
//設(shè)置通知圖標
.setSmallIcon(R.drawable.seek02)
//設(shè)置通知內(nèi)容標題
.setContentTitle("該應用發(fā)生 爆炸大 大 大 新聞!!")
//設(shè)置通知內(nèi)容
.setContentText("冒險沒有 你手機自嗨罷了~")
//設(shè)置使用默認的聲音 LED燈
.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS)
//設(shè)置通知自定義聲音
// .setSound()
.setWhen(System.currentTimeMillis())
//設(shè)置他只要啟動的程序Intent
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(NOTIFICATION_ID,notification);
}
public void del(View view){
//取消通知
notificationManager.cancel(NOTIFICATION_ID);
}
}
2.然后建立一個要打開的活動(隨意建就行)(布局文件任意我這里就不寫了)
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)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android開發(fā)實現(xiàn)讀取assets目錄下db文件的方法示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)讀取assets目錄下db文件的方法,結(jié)合實例形式分析了Android針對assets目錄下SQLite數(shù)據(jù)庫文件的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android Shader應用開發(fā)之霓虹閃爍文字效果
這篇文章主要為大家詳細介紹了Android Shader應用開發(fā)之霓虹閃爍文字效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Flutter banner_view 輪播圖的使用及實現(xiàn)代碼
這篇文章主要介紹了Flutter banner_view 輪播圖的使用及實現(xiàn)代碼,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-07-07

