android 本地提醒功能,android中的本地定时推送到通知栏
一、使用系統(tǒng)定義的Notification
以下是使用示例代碼:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
public class WaterActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mian_water);
//獲取到通知管理器
manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_wf_back:
// 定義Notification的各種屬性
int icon = R.drawable.button_login; //通知圖標(biāo)
CharSequence tickerText = "Hello"; //狀態(tài)欄顯示的通知文本提示
long when = System.currentTimeMillis(); //通知產(chǎn)生的時(shí)間,會(huì)在通知信息里顯示
Notification myNotify = new Notification(icon,tickerText,when);
Context context = getApplicationContext(); //上下文
CharSequence contentTitle = "My Notification"; //通知欄標(biāo)題
CharSequence contentText = "Hello World!"; //通知欄內(nèi)容
Intent notificationIntent = new Intent(this,WaterActivity.class); //點(diǎn)擊該通知后要跳轉(zhuǎn)的Activity
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
myNotify.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
manager.notify(0x00000008, myNotify);
//如果想要更新一個(gè)通知,只需要在設(shè)置好notification之后,再次調(diào)用 setLatestEventInfo(),然后重新發(fā)送一次通知即可,即再次調(diào)用notify()。
break;
}
}
}
二、使用自定義的 Notification
要?jiǎng)?chuàng)建一個(gè)自定義的Notification,可以使用RemoteViews。
要定義自己的擴(kuò)展消息,首先 要初始化一個(gè)RemoteViews對(duì)象,然后將它傳遞給Notification的contentView字段,再把PendingIntent傳遞給 contentIntent字段。
以下示例代碼是完整步驟:
1、創(chuàng)建一個(gè)自 定義的消息布局 my_notification.xml
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical" >
android:id="@+id/text_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
2、 在程序代碼中使用RemoteViews的方法來(lái)定義image和text。然后把RemoteViews對(duì)象傳到contentView字段
RemoteViews rv = new RemoteViews(getPackageName(), R.layout.my_notification);
rv.setTextViewText(R.id.text_content, "hello wrold!");
myNotify.contentView = rv;
3、 為Notification的contentIntent字段定義一個(gè)Intent(注意,使用自定義View不需要 setLatestEventInfo()方法)
Intent intent = new Intent(Intent.ACTION_MAIN);
PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1);
myNotify.contentIntent = contentIntent;
4、發(fā)送通知
manager.notify(0x00000008, myNotify);
5.完整代碼
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.widget.RemoteViews;
public class WaterActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mian_water);
//獲取到通知管理器
manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_wf_back:
Notification myNotify = new Notification();
myNotify.icon = R.drawable.button_login;
myNotify.tickerText = "TickerText:您有新短消息,請(qǐng)注意查收!";
myNotify.when = System.currentTimeMillis();
//myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動(dòng)清除
RemoteViews rv = new RemoteViews(getPackageName(), R.layout.my_notification);
rv.setTextViewText(R.id.text_content, "hello wrold!");
myNotify.contentView = rv;
Intent intent = new Intent(Intent.ACTION_MAIN);
PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1);
myNotify.contentIntent = contentIntent;
manager.notify(0x00000008, myNotify);
//如果想要更新一個(gè)通知,只需要在設(shè)置好notification之后,再次調(diào)用 setLatestEventInfo(),然后重新發(fā)送一次通知即可,即再次調(diào)用notify()。
break;
}
}
}
6.清除
manager.cancel(2);
參數(shù)屬性:
// 定義Notification的各種屬性
Notification notification =new Notification(R.drawable.icon,
"測(cè)試", System.currentTimeMillis());
//FLAG_AUTO_CANCEL 該通知能被狀態(tài)欄的清除按鈕給清除掉
//FLAG_NO_CLEAR 該通知不能被狀態(tài)欄的清除按鈕給清除掉
//FLAG_ONGOING_EVENT 通知放置在正在運(yùn)行
//FLAG_INSISTENT 是否一直進(jìn)行,比如音樂(lè)一直播放,知道用戶響應(yīng)
notification.flags |= Notification.FLAG_ONGOING_EVENT;
// 將此通知放到通知欄的"Ongoing"即"正在運(yùn)行"組中
notification.flags |= Notification.FLAG_NO_CLEAR;
// 表明在點(diǎn)擊了通知欄中的"清除通知"后,此通知不清除,經(jīng)常與FLAG_ONGOING_EVENT一起使用
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//DEFAULT_ALL 使用所有默認(rèn)值,比如聲音,震動(dòng),閃屏等等
//DEFAULT_LIGHTS 使用默認(rèn)閃光提示
//DEFAULT_SOUNDS 使用默認(rèn)提示聲音
//DEFAULT_VIBRATE 使用默認(rèn)手機(jī)震動(dòng),需加上權(quán)限
notification.defaults = Notification.DEFAULT_LIGHTS;
//疊加效果常量
//notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
notification.ledARGB = Color.BLUE;
notification.ledOnMS =5000; //閃光時(shí)間,毫秒
// 設(shè)置通知的事件消息
CharSequence contentTitle ="標(biāo)題"; // 通知欄標(biāo)題
CharSequence contentText ="內(nèi)容"; // 通知欄內(nèi)容
//如果需要跳轉(zhuǎn)到指定的Activity,則需要設(shè)置PendingIntent
Intent notificationIntent =new Intent(A.this, B.class);
// 點(diǎn)擊該通知后要跳轉(zhuǎn)的Activity
notificationIntent.putExtra("date","需要傳遞的參數(shù)");
// FLAG_UPDATE_CURRENT 更新數(shù)據(jù),如果有多個(gè)PendingIntent,且requestCode相同,則會(huì)替換為最新extra數(shù)據(jù)
//如果需要通過(guò)不同的extra數(shù)據(jù),進(jìn)行處理,就需要requestCode不相同
int requestCode = new Random().nextInt();
PendingIntent contentItent = PendingIntent.getActivity(this, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);
// 把Notification傳遞給NotificationManager
notificationManager.notify(0, notification);
注意:
new Intent(this,this.getClass())保證了點(diǎn)擊通知欄里的通知可以回到該Activity
但是,假如該Activity還在后臺(tái)運(yùn)行,并沒(méi)有運(yùn)行,通知事件響應(yīng)后,系統(tǒng)會(huì)自動(dòng)結(jié)束該Activity,然后再重新啟動(dòng)Activity,這不是我們要的。
解決方法為:在manifest.xml文件中找到該Activity,添加屬性android:launchMode="singleTask“。這個(gè)屬性很明顯,就是只允許有一個(gè)該Activity運(yùn)行,如果正在運(yùn)行,則只能切換到當(dāng)前運(yùn)行的Activity,而不能重新啟動(dòng)Activity。
三、創(chuàng)建定時(shí)器
源代碼如下:
import java.util.Timer;
import java.util.TimerTask;
public class WaterActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {
private Timer mTimer = null;
private TimerTask mTimerTask = null;
private int isPause = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mian_water);
}
private void startMyTimer(){
if (mTimer == null) {
mTimer = new Timer();
}
if (mTimerTask == null) {
mTimerTask = new TimerTask() {
@Override
public void run() {
do {
try {
if(isPause == 0) isPause = 1;
else isPause = 0;
Message message = new Message();
message.what = isPause;
handler.sendMessage(message);
} catch (IllegalStateException e) {
}
} while (false);
}
};
}
if(mTimer != null && mTimerTask != null )
mTimer.schedule(mTimerTask, 0, 500);
}
private void stopMyTimer(){
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
if (mTimerTask != null) {
mTimerTask.cancel();
mTimerTask = null;
}
}
}
在Unity3D中實(shí)現(xiàn)安卓平臺(tái)的本地通知推送
[前言] 對(duì)于手游來(lái)說(shuō),什么時(shí)候需要推送呢?玩過(guò)一些帶體力限制的游戲就會(huì)發(fā)現(xiàn),我的體力在恢復(fù)滿后,手機(jī)會(huì)收到一個(gè)通知告訴我體力已完全恢復(fù)了.這類通知通常是由本地的客戶端發(fā)起的,沒(méi)有經(jīng)過(guò)服務(wù)端. 在安卓 ...
Android本地消息推送
項(xiàng)目介紹:cocos2dx跨平臺(tái)游戲 項(xiàng)目需求:實(shí)現(xiàn)本地消息推送,需求①:定點(diǎn)推送:需求②:根據(jù)游戲內(nèi)邏輯實(shí)現(xiàn)推送(比如玩家體力滿時(shí),需要計(jì)算后到點(diǎn)推送):需求③:清理后臺(tái)程序或重啟后依然能夠?qū)崿F(xiàn)本地 ...
Android 基于Netty的消息推送方案之Hello World(一)
消息推送方案(輪詢.長(zhǎng)連接) 輪詢 輪詢:比較簡(jiǎn)單的,最容易理解和實(shí)現(xiàn)的就是客戶端去服務(wù)器上拉信息,信息的及時(shí)性要求越高則拉信息的頻率越高.客戶端拉信息的觸發(fā)可以是一些事件,也可以是一個(gè)定時(shí)器,不斷地 ...
IOS 本地通知推送消息
在現(xiàn)在的移動(dòng)設(shè)備中,好多應(yīng)用性的APP都用到了推送服務(wù),但是有好多推送的內(nèi)容,比如有的只是單純的進(jìn)行推送一個(gè)鬧鐘類型的,起了提醒作 用,有的則是推送的實(shí)質(zhì)性的內(nèi)容,這就分為推送的內(nèi)容來(lái)區(qū)別用什么推送, ...
Android 基于Netty的消息推送方案之對(duì)象的傳遞(四)
在上一篇文章中我們介紹了Netty的字符串傳遞,我們知道了Netty的消息傳遞都是基于流,通過(guò)ChannelBuf ...
Android 基于Netty的消息推送方案之字符串的接收和發(fā)送(三)
在上一篇文章中?,我們介紹過(guò)一些關(guān)于Netty的概念和工作原理的內(nèi)容,今天我們先來(lái)介紹一個(gè)叫做ChannelBuffe ...
Android 基于Netty的消息推送方案之概念和工作原理(二)
上一篇文章中我講述了關(guān)于消息推送的方案以及一個(gè)基于Netty實(shí)現(xiàn)的一個(gè)簡(jiǎn)單的Hello World,為了更好的理解Hello World中的代碼,今天我來(lái)講解一下關(guān)于Netty中一些概念和工作原理的內(nèi) ...
Git總結(jié)筆記3-把本地倉(cāng)庫(kù)推送到github
說(shuō)明:此筆記在centos 7 上完成 1.配置公鑰 [root@kangvcar ~]# ssh-keygen -t rsa -C "kangvcar@126.com" [roo ...
WebSocket(4)---實(shí)現(xiàn)定時(shí)推送比特幣交易信息
實(shí)現(xiàn)定時(shí)推送比特幣交易信息 實(shí)現(xiàn)功能:跟虛擬幣交易所一樣,時(shí)時(shí)更新當(dāng)前比特幣的價(jià)格,最高價(jià),最低價(jià),買一價(jià)等等...... 提示:(1)本篇博客是在上一遍基礎(chǔ)上搭建,上一篇博客地址:[WebSocke ...
隨機(jī)推薦
[APUE]文件和目錄(上)
一.文件權(quán)限 1. 各種ID 我在讀這一章時(shí)遇到了各種ID,根據(jù)名字完全不清楚什么意思,幸好看到了這篇文章,http://blog.csdn.net/ccjjnn19890720/article/de ...
Webservice接口
快遞查詢接口 http://webservice.36wu.com/ExpressService.asmxip查詢接口 http://webservice.36wu.com/ipService.asm ...
freeCodeCamp:Diff Two Arrays
比較兩個(gè)數(shù)組,然后返回一個(gè)新數(shù)組,該數(shù)組的元素為兩個(gè)給定數(shù)組中所有獨(dú)有的數(shù)組元素.換言之,返回兩個(gè)數(shù)組的差異. function diff(arr1, arr2) { var newArr = []; ...
CoreAnimation 核心動(dòng)畫二 錨點(diǎn)
錨點(diǎn): anchorPoint ? ? 以錨點(diǎn)為中心 執(zhí)行動(dòng)畫 (與 漁夫固定船的點(diǎn)時(shí)一致的) anchorPoint 默認(rèn)是 0.5,0.5? (注意: 錨點(diǎn) 是一個(gè)比例) anchorPoint ...
GitHub與VS2013完成項(xiàng)目管理
https://github.com 程序員應(yīng)該去注冊(cè)一個(gè)賬號(hào)的網(wǎng)站 1.創(chuàng)建一個(gè)倉(cāng)庫(kù) 登錄你的github網(wǎng)站:找到新建一個(gè)倉(cāng)庫(kù)的入口 一些基本信息填寫完畢后,點(diǎn)擊創(chuàng)建,即可擁有一個(gè)倉(cāng)庫(kù) 2. 讓V ...
iOS之創(chuàng)建一個(gè)常駐線程
// 當(dāng)創(chuàng)建一個(gè)線程,并且希望它一直存在時(shí),但往往我們創(chuàng)建的線程都是執(zhí)行完成之后也就停止了,不能再次利用,那么如何創(chuàng)建一個(gè)線程可以讓他可以再次工作呢,這個(gè)時(shí)候就需要使用到RunLoop了.下面的是我寫 ...
天氣類App原型制作分享-ColorfulClouds
ColorfulClouds是一款界面精美的天氣預(yù)報(bào)App,它可以準(zhǔn)確預(yù)報(bào)降雨量.污染程度等.這款A(yù)pp最美的是它的首頁(yè)天氣插畫,扁平精美,同時(shí)配上了適當(dāng)?shù)膭?dòng)效,把普通的天氣變得漂亮有趣,十分吸引眼球 ...
在Ubuntu下運(yùn)行 apt-get update命令后出現(xiàn)錯(cuò)誤:
在Ubuntu下運(yùn)行 apt-get update命令后出現(xiàn)錯(cuò)誤:?The package lists or status file could not be parsed or opened sud ...
datagridview的一些設(shè)置
1.自動(dòng)調(diào)整列寬 this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMo ...
Git 中 pull 和 clone 的區(qū)別
git pull git clone clone 是本地沒(méi)有 repository 時(shí),將遠(yuǎn)程 repository 整個(gè)下載過(guò)來(lái). pull 是本地有 repository 時(shí),將遠(yuǎn)程 reposi ...
總結(jié)
以上是生活随笔為你收集整理的android 本地提醒功能,android中的本地定时推送到通知栏的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 进行判断使用class_记一次使用 Ar
- 下一篇: sklearn tfidf求余弦相似度_