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