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