Android中通過Notification&NotificationManager實(shí)現(xiàn)消息通知
notification是一種讓你的應(yīng)用程序在沒有開啟情況下或在后臺運(yùn)行警示用戶。它是看不見的程序組件(Broadcast Receiver,Service和不活躍的Activity)警示用戶有需要注意的事件發(fā)生的最好途徑。
1、新建一個android項(xiàng)目
我新建項(xiàng)目的 minSdkVersion="11",targetSdkVersion="19"。也就是支持最低版本的3.0的。
2、習(xí)慣性地打開項(xiàng)目清單文件AndroidManifest.xml,添加一個權(quán)限:<uses-permission android:name="android.permission.VIBRATE"/> 不添加不行的。
3、在布局activity_main.xml中添加幾個按鈕,樣子就大概這樣,垂直排版的LinearLayout
具體代碼
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3.0以前版本的notification,用新的吧"
android:onClick="click"
/>
<Button
android:id="@+id/btn_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大視圖文本通知"
android:onClick="click"
/>
<Button
android:id="@+id/btn_03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大視圖圖片通知"
android:onClick="click"
/>
<Button
android:id="@+id/btn_04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="進(jìn)度條通知"
android:onClick="click"
/>
</LinearLayout>
4、MainActivity中的代碼:
package com.xin.day__notificationdemo;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v.app.NotificationCompat;
import android.support.v.app.NotificationCompat.BigPictureStyle;
import android.support.v.app.NotificationCompat.BigTextStyle;
import android.support.v.app.NotificationCompat.Builder;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
//通知的唯一標(biāo)識,在一個應(yīng)用程序中不同的通知要區(qū)別開來
private static final int NO = x;
private static final int NO = x;
private static final int NO = x;
private static final int NO = x;
//進(jìn)度條要用
private int progress = ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//click方法,和xml文件中的各個按鈕的onClick屬性的值要一致
public void click(View view) {
//創(chuàng)建NotificationManager
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//用switch語句控制四個控件
switch (view.getId()) {
case R.id.btn_: {
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "有消息了。。。";
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, ,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, ".以前的通知", "試試而已", pendingIntent);
notification.when = System.currentTimeMillis();
notification.defaults = Notification.DEFAULT_ALL;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.number = ;
notification.vibrate = new long[]{, };
manager.notify(NO, notification);
}
break;
case R.id.btn_:{
//大視圖文本通知
//創(chuàng)建消息構(gòu)造器,在擴(kuò)展包
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//設(shè)置當(dāng)有消息是的提示,圖標(biāo)和提示文字
builder.setSmallIcon(R.drawable.ic_launcher).setTicker("有新消息了");
//需要樣式
BigTextStyle style = new BigTextStyle();
style.setBigContentTitle("上課通知");//通知的標(biāo)題
style.bigText("今天下午要在綜B上jsp");//通知的文本內(nèi)容
//大視圖文本具體內(nèi)容
style.setSummaryText("這是正常的課程安排,請各位同學(xué)按時上課");
builder.setStyle(style);
//顯示消息到達(dá)的時間,這里設(shè)置當(dāng)前時間
builder.setWhen(System.currentTimeMillis());
//獲取一個通知對象
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
//發(fā)送(顯示)通知
//notify()第一個參數(shù)id An identifier for this notification unique within your application
//get?意思說,這個通知在你的應(yīng)用程序中唯一的標(biāo)識符
manager.notify(NO, notification);
}
break;
case R.id.btn_:{
//大視圖圖片通知
NotificationCompat.Builder builderPic = new Builder(this);
builderPic.setSmallIcon(R.drawable.ic_launcher).setTicker("新浪體育提醒");
//進(jìn)行設(shè)置
BigPictureStyle pictureStyle = new BigPictureStyle();
pictureStyle.setBigContentTitle("新浪體育 快船VS騎士 ");
pictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_game));
pictureStyle.setSummaryText(" 快船VS騎士 天王山之戰(zhàn)?。?!");//不要在意文字
//設(shè)置樣式
builderPic.setStyle(pictureStyle);
//設(shè)置顯示的時間
builderPic.setWhen(System.currentTimeMillis());
Notification notification = pictureStyle.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
//
manager.notify(NO, notification);
}
break;
case R.id.btn_:{
//進(jìn)度條通知
final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(this);
builderProgress.setSmallIcon(R.drawable.ic_launcher).setTicker("進(jìn)度條通知");
builderProgress.setProgress(, progress, false);
final Notification notification = builderProgress.build();
//發(fā)送一個通知
manager.notify(NO, notification);
//創(chuàng)建一個計(jì)時器
Timer timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() {
Log.i("progress",progress+"");
while(progress <= ){
progress ++;
try {
Thread.sleep();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//更新進(jìn)度條
builderProgress.setProgress(, progress, false);
//再次通知
manager.notify(NO, builderProgress.build());
}
//計(jì)時器退出
this.cancel();
//進(jìn)度條退出
manager.cancel(NO);
return;//結(jié)束方法
}
}, );
}
break;
default:
break;
}
}
}
5、運(yùn)行:我的虛擬機(jī)版本是4.0的(api19),按住通知左(右)滑動就可以讓通知小時了。
效果如下:

- Android編程實(shí)現(xiàn)google消息通知功能示例
- Android之開發(fā)消息通知欄
- Android消息通知欄的實(shí)現(xiàn)方法介紹
- Android自定義Notification添加點(diǎn)擊事件
- Android中AlarmManager+Notification實(shí)現(xiàn)定時通知提醒功能
- Android 中Notification彈出通知實(shí)現(xiàn)代碼
- Android編程使用Service實(shí)現(xiàn)Notification定時發(fā)送功能示例
- Android 通知使用權(quán)(NotificationListenerService)的使用
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android消息通知Notification常用方法(發(fā)送消息和接收消息)
相關(guān)文章
淺談Android應(yīng)用安全防護(hù)和逆向分析之a(chǎn)pk反編譯
我們有時候在某個app上見到某個功能,某個效果蠻不錯的,我們想看看對方的思路怎么走的,這時候,我們就可以通過反編譯來編譯該apk,拿到代碼,進(jìn)行分析。2021-06-06
android使用viewpager計(jì)算偏移量實(shí)現(xiàn)選項(xiàng)卡功能
這篇文章主要為大家詳細(xì)介紹了android使用viewpager計(jì)算偏移量實(shí)現(xiàn)選項(xiàng)卡功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
Android RecyclerView實(shí)現(xiàn)下拉列表功能
這篇文章主要介紹了Android RecyclerView實(shí)現(xiàn)下拉列表功能,下拉展開更多選項(xiàng),具有一定的實(shí)用性,感興趣的小伙伴們可以參考一下2016-11-11
ComposeDesktop開發(fā)桌面端多功能APK工具
這篇文章主要為大家介紹了ComposeDesktop開發(fā)桌面端多功能APK工具實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Android邊播放邊緩存視頻框架AndroidVideoCache詳解
這篇文章主要為大家介紹了Android邊播放邊緩存視頻框架AndroidVideoCache詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android工具類ImgUtil選擇相機(jī)和系統(tǒng)相冊
這篇文章主要為大家詳細(xì)介紹了Android工具類ImgUtil選擇相機(jī)和系統(tǒng)相冊,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
Android UI設(shè)計(jì)系列之ImageView實(shí)現(xiàn)ProgressBar旋轉(zhuǎn)效果(1)
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)之ImageView實(shí)現(xiàn)ProgressBar旋轉(zhuǎn)效果,具有一定的實(shí)用性和參考價值,感興趣的小伙伴們可以參考一下2016-06-06
Android7.0指紋服務(wù)FingerprintService實(shí)例介紹
這篇文章主要介紹了Android7.0指紋服務(wù)FingerprintService介紹,需要的朋友可以參考下2018-01-01
Android實(shí)現(xiàn)View拖拽跟隨手指移動效果
這篇文章主要介紹了Android實(shí)現(xiàn)View拖拽跟隨手指移動效果,主要使用setTranslationX() 和setTranslationY() 屬性方法實(shí)現(xiàn)的,需要的朋友參考下吧2017-08-08

