Android中使用Notification實(shí)現(xiàn)狀態(tài)欄的通知
在使用手機(jī)時(shí),當(dāng)有未接來(lái)電或者新短消息時(shí),手機(jī)會(huì)給出響應(yīng)的提示信息,這些提示信息通常會(huì)顯示到手機(jī)屏幕的狀態(tài)欄上。
Android也提供了用于處理這些信息的類(lèi),它們是Notification和NotificationManager。其中,Notification代表的是具有全局效果的通知,而NotificationManager則是用于發(fā)送Notification通知的系統(tǒng)服務(wù)。
使用Notification和NotificationManager類(lèi)發(fā)送和顯示通知也比較簡(jiǎn)單,大致可以分為以下四個(gè)步驟
(1)調(diào)用getSystemService() 方法獲取系統(tǒng)的NotificationManager服務(wù)
(2)創(chuàng)建一個(gè)Notification對(duì)象,并為其設(shè)置各種屬性
(3)為Notification對(duì)象設(shè)置事件信息
(4)通過(guò)NotificationManager類(lèi)的notify()方法發(fā)送Notification通知
下面通過(guò)一個(gè)實(shí)例說(shuō)明和使用Notification在狀態(tài)欄上顯示通知
國(guó)際慣例
運(yùn)行結(jié)果:

布局文件就不發(fā)了 線性垂直布局 兩個(gè)按鈕
MainActivity.class
package com.example.notification;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private NotificationManager manager;
private Button button1;
private Button button2;
private int Notification_ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:{
showNotification();
break;
}
case R.id.button2:{
manager.cancel(Notification_ID);
break;
}
}
}
private void showNotification() {
// TODO Auto-generated method stub
Notification.Builder builder=new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);//設(shè)置圖標(biāo)
builder.setTicker("通知來(lái)啦");//手機(jī)狀態(tài)欄的提示
builder.setContentTitle("我是通知標(biāo)題");//設(shè)置標(biāo)題
builder.setContentText("我是通知內(nèi)容");//設(shè)置通知內(nèi)容
builder.setWhen(System.currentTimeMillis());//設(shè)置通知時(shí)間
Intent intent=new Intent(this,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);//點(diǎn)擊后的意圖
builder.setDefaults(Notification.DEFAULT_LIGHTS);//設(shè)置指示燈
builder.setDefaults(Notification.DEFAULT_SOUND);//設(shè)置提示聲音
builder.setDefaults(Notification.DEFAULT_VIBRATE);//設(shè)置震動(dòng)
Notification notification=builder.build();//4.1以上,以下要用getNotification()
manager.notify(Notification_ID, notification);
}
}
上面代碼中設(shè)置的指示燈和震動(dòng),由于程序中要訪問(wèn)系統(tǒng)的指示燈和振動(dòng)器 所以要在AndroidManifest.xml中聲明使用權(quán)限
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.FLASHLIGHT" />
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- Android使用Notification實(shí)現(xiàn)通知功能
- Android開(kāi)發(fā)之Notification手機(jī)狀態(tài)欄通知用法實(shí)例分析
- Android使用Notification在狀態(tài)欄上顯示通知
- Android中Notification通知用法詳解
- Android 中Notification彈出通知實(shí)現(xiàn)代碼
- android 通知Notification詳解及實(shí)例代碼
- Android開(kāi)發(fā)之Notification通知用法詳解
- Android中通知Notification的使用方法
- Android Notification通知使用詳解
相關(guān)文章
Android ImageView的selector效果實(shí)例詳解
這篇文章主要介紹了Android ImageView的selector效果實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
Handler制作簡(jiǎn)單相冊(cè)查看器的實(shí)例代碼
下面小編就為大家分享一篇Handler制作簡(jiǎn)單相冊(cè)查看器的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android實(shí)戰(zhàn)打飛機(jī)游戲之菜單頁(yè)面設(shè)計(jì)(1)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)打飛機(jī)游戲之菜單頁(yè)面設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
android listview進(jìn)階實(shí)例分享
這篇文章主要介紹了android listview進(jìn)階實(shí)例分享,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Android開(kāi)發(fā)中Flutter組件實(shí)用技巧
這篇文章主要為大家介紹了Android開(kāi)發(fā)中Flutter組件實(shí)用技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
android 圖片操作(縮放移動(dòng)) 實(shí)例代碼
android 圖片操作(縮放移動(dòng)) 實(shí)例代碼,需要的朋友可以參考一下2013-06-06
Android 中使用EditText 點(diǎn)擊全選再次點(diǎn)擊取消全選功能
這篇文章主要介紹了Android 中使用EditText 點(diǎn)擊全選再次點(diǎn)擊取消全選功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-12-12
微信小程序電商常用倒計(jì)時(shí)實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了微信小程序電商常用倒計(jì)時(shí)實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06

