android 通知Notification詳解及實(shí)例代碼
android Notification實(shí)例詳解
1.使用Builder模式來創(chuàng)建
2.必須要設(shè)置一個(gè)smallIcon,還可以設(shè)置setTicker
3.可以設(shè)置 setContentTitle,setContentInfo,setContentText,setWhen
4.可以設(shè)置setDefaults(閃屏,聲音,震動(dòng)),通過Notification設(shè)置flags(能不能被清除)
5.發(fā)送需要獲取一個(gè)NotificationManager(getSystemService來獲取);notify(int id,Notification)
6.清除 manager.cancelAll(清除所有) cancal(int id); 如果需要16一下的api也能訪問,需要導(dǎo)入v4包,使用notificationCompat兼容類。
自定義通知
使用RemoteViews來建立一個(gè)布局,然后使用setContent()設(shè)置;
點(diǎn)擊事件使用PendingIntent來完成
下面是一個(gè)案例


MainActivity類
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clearNotification(View v) {
// 通過代碼來清除 NO_CLEAR
// 清除也需要manager
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 只能清除由本應(yīng)用程序發(fā)出去的通知
manager.cancelAll();
}
public void sendNotification(View v) {
// 他是 在v4包中的通知,用于16以下版本發(fā)送通知
// NotificationCompat
// 通知的創(chuàng)建
Notification.Builder builder = new Notification.Builder(this);
// NotificationCompat.Builder builder2 = new NotificationCompat.Builder(
// this);
// 顯示在通知條上的圖標(biāo) 必須的
builder.setSmallIcon(R.drawable.ic_launcher);
// 顯示通知條上的文字 不是必須的
builder.setTicker("您有一條新的消息!!");
// 狀態(tài)欄中的
builder.setContentTitle("大標(biāo)題");
builder.setContentText("文本");
builder.setWhen(System.currentTimeMillis());
builder.setContentInfo("Info");
builder.setDefaults(Notification.DEFAULT_ALL);
// 點(diǎn)擊事件使用Pending
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 1, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
// 生成對(duì)象 16API以上,支持低版本需要使用v4包中的notificationCompat
Notification notify = builder.build();
// 設(shè)置不能清除
notify.flags = Notification.FLAG_NO_CLEAR;
// 如何將通知發(fā)送出去
NotificationManager mananger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 通知的唯一值,如果id重復(fù),表明是更新一條通知,而不是新建
mananger.notify((int) (Math.random() * 1000), notify);
}
public void diyNotification(View v) {
// 展示在通知上面的視圖
RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher).setTicker("自定義通知 ")
// 布局
.setContent(views).build();
// 使用RemoteViews來設(shè)置點(diǎn)擊事件
views.setTextColor(R.id.tv, Color.RED);
Intent intent = new Intent(this, OneActivity.class);
// 音樂播放是放在Service
PendingIntent pi = PendingIntent.getActivity(this, 2, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.tv, pi);
Intent intent2 = new Intent(this, MainActivity.class);
PendingIntent pi2 = PendingIntent.getActivity(this, 1, intent2,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.iv, pi2);
// 發(fā)送
NotificationManager notify = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notify.notify(1, notification);
}
}
OneActivity類
public class OneActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("跳轉(zhuǎn)界面");
setContentView(tv);
}
}
activity.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lesson8_notification.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendNotification"
android:text="普通的通知" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clearNotification"
android:text="清除所有通知" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="diyNotification"
android:text="自定義通知" />
</LinearLayout>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
最后記得注冊activity
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android使用Notification實(shí)現(xiàn)通知功能
- Android開發(fā)之Notification手機(jī)狀態(tài)欄通知用法實(shí)例分析
- Android使用Notification在狀態(tài)欄上顯示通知
- Android中Notification通知用法詳解
- Android 中Notification彈出通知實(shí)現(xiàn)代碼
- Android中使用Notification實(shí)現(xiàn)狀態(tài)欄的通知
- Android開發(fā)之Notification通知用法詳解
- Android中通知Notification的使用方法
- Android Notification通知使用詳解
相關(guān)文章
Android Imageloader的配置的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android Imageloader的配置的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07
ListView點(diǎn)擊Item展開菜單實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了ListView點(diǎn)擊Item展開菜單實(shí)現(xiàn)代碼詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
Android 圖片的三級(jí)緩存機(jī)制實(shí)例分析
這篇文章主要介紹了Android 圖片的三級(jí)緩存機(jī)制實(shí)例分析的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android中用Bmob實(shí)現(xiàn)短信驗(yàn)證碼功能的方法詳解
本文給大家分享通過第三方平臺(tái)Bmob實(shí)現(xiàn)發(fā)送驗(yàn)證碼和校驗(yàn)驗(yàn)證碼的功能,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09
android studio 4.0 新建類沒有修飾符的方法
這篇文章主要介紹了android studio 4.0 新建類沒有修飾符的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Android編程實(shí)現(xiàn)ListView頭部ViewPager廣告輪詢圖效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)ListView頭部ViewPager廣告輪詢圖效果,較為詳細(xì)的分析了自定義ListView實(shí)現(xiàn)ViewPager廣告圖輪詢的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android開發(fā)之imageView圖片按比例縮放的實(shí)現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之imageView圖片按比例縮放的實(shí)現(xiàn)方法,較為詳細(xì)的分析了Android中ImageView控件的scaleType屬性控制圖片縮放的具體用法,需要的朋友可以參考下2016-01-01

