Android編程使用Service實(shí)現(xiàn)Notification定時發(fā)送功能示例
本文實(shí)例講述了Android編程使用Service實(shí)現(xiàn)Notification定時發(fā)送功能。分享給大家供大家參考,具體如下:
/**
* 通過啟動或停止服務(wù)來管理通知功能
*
* @description:
* @author ldm
* @date 2016-4-29 上午9:15:15
*/
public class NotifyControlActivity extends Activity {
private Button notifyStart;// 啟動通知服務(wù)
private Button notifyStop;// 停止通知服務(wù)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notifying_controller);
initWidgets();
}
private void initWidgets() {
notifyStart = (Button) findViewById(R.id.notifyStart);
notifyStart.setOnClickListener(mStartListener);
notifyStop = (Button) findViewById(R.id.notifyStop);
notifyStop.setOnClickListener(mStopListener);
}
private OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
// 啟動Notification對應(yīng)Service
startService(new Intent(NotifyControlActivity.this,
NotifyingService.class));
}
};
private OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
// 停止Notification對應(yīng)Service
stopService(new Intent(NotifyControlActivity.this,
NotifyingService.class));
}
};
}
/**
* 實(shí)現(xiàn)每5秒發(fā)一條狀態(tài)欄通知的Service
*
* @description:
* @author ldm
* @date 2016-4-29 上午9:16:20
*/
public class NotifyingService extends Service {
// 狀態(tài)欄通知的管理類對象,負(fù)責(zé)發(fā)通知、清楚通知等
private NotificationManager mNM;
// 使用Layout文件的對應(yīng)ID來作為通知的唯一識別
private static int MOOD_NOTIFICATIONS = R.layout.status_bar_notifications;
/**
* Android給我們提供ConditionVariable類,用于線程同步。提供了三個方法block()、open()、close()。 void
* block() 阻塞當(dāng)前線程,直到條件為open 。 void block(long timeout)阻塞當(dāng)前線程,直到條件為open或超時
* void open()釋放所有阻塞的線程 void close() 將條件重置為close。
*/
private ConditionVariable mCondition;
@Override
public void onCreate() {
// 狀態(tài)欄通知的管理類對象,負(fù)責(zé)發(fā)通知、清楚通知等
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 啟動一個新個線程執(zhí)行任務(wù),因Service也是運(yùn)行在主線程,不能用來執(zhí)行耗時操作
Thread notifyingThread = new Thread(null, mTask, "NotifyingService");
mCondition = new ConditionVariable(false);
notifyingThread.start();
}
@Override
public void onDestroy() {
// 取消通知功能
mNM.cancel(MOOD_NOTIFICATIONS);
// 停止線程進(jìn)一步生成通知
mCondition.open();
}
/**
* 生成通知的線程任務(wù)
*/
private Runnable mTask = new Runnable() {
public void run() {
for (int i = 0; i < 4; ++i) {
// 生成帶stat_happy及status_bar_notifications_happy_message內(nèi)容的通知
showNotification(R.drawable.stat_happy,
R.string.status_bar_notifications_happy_message);
if (mCondition.block(5 * 1000))
break;
// 生成帶stat_neutral及status_bar_notifications_ok_message內(nèi)容的通知
showNotification(R.drawable.stat_neutral,
R.string.status_bar_notifications_ok_message);
if (mCondition.block(5 * 1000))
break;
// 生成帶stat_sad及status_bar_notifications_sad_message內(nèi)容的通知
showNotification(R.drawable.stat_sad,
R.string.status_bar_notifications_sad_message);
if (mCondition.block(5 * 1000))
break;
}
// 完成通知功能,停止服務(wù)。
NotifyingService.this.stopSelf();
}
};
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@SuppressWarnings("deprecation")
private void showNotification(int moodId, int textId) {
// 自定義一條通知內(nèi)容
CharSequence text = getText(textId);
// 當(dāng)點(diǎn)擊通知時通過PendingIntent來執(zhí)行指定頁面跳轉(zhuǎn)或取消通知欄等消息操作
Notification notification = new Notification(moodId, null,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, NotifyControlActivity.class), 0);
// 在此處設(shè)置在nority列表里的該norifycation得顯示情況。
notification.setLatestEventInfo(this,
getText(R.string.status_bar_notifications_mood_title), text,
contentIntent);
/**
* 注意,我們使用出來。incoming_message ID 通知。它可以是任何整數(shù),但我們使用 資源id字符串相關(guān)
* 通知。它將永遠(yuǎn)是一個獨(dú)特的號碼在你的 應(yīng)用程序。
*/
mNM.notify(MOOD_NOTIFICATIONS, notification);
}
// 這是接收來自客戶端的交互的對象. See
private final IBinder mBinder = new Binder() {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
return super.onTransact(code, data, reply, flags);
}
};
}
<?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:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip"
android:text="通過Service來實(shí)現(xiàn)對Notification的發(fā)送管理" />
<Button
android:id="@+id/notifyStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啟動服務(wù)" >
<requestFocus />
</Button>
<Button
android:id="@+id/notifyStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服務(wù)" >
</Button>
</LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android資源操作技巧匯總》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android入門之Service的使用詳解
- Android?NotificationListenerService通知監(jiān)聽服務(wù)使用
- Android Google AutoService框架使用詳解
- Android使用Service實(shí)現(xiàn)IPC通信的2種方式
- 說說在Android如何使用服務(wù)(Service)的方法
- Android使用Service實(shí)現(xiàn)簡單音樂播放實(shí)例
- 淺談Android中Service的注冊方式及使用
- Android 通知使用權(quán)(NotificationListenerService)的使用
- Android Service功能使用示例代碼
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)ListView點(diǎn)擊展開收起效果示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)ListView點(diǎn)擊展開收起效果,結(jié)合實(shí)例形式分析了Android ListView控件的布局及事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Android實(shí)現(xiàn)蝸牛進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)蝸牛進(jìn)度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
android中實(shí)現(xiàn)OkHttp下載文件并帶進(jìn)度條
本篇文章主要介紹了android中實(shí)現(xiàn)OkHttp下載文件并帶進(jìn)度條,OkHttp是比較火的網(wǎng)絡(luò)框架,它支持同步與異步請求,支持緩存,可以攔截,更方便下載大文件與上傳文件的操作,有興趣的可以了解一下2017-07-07
android聊天界面鍵盤、表情切換絲滑實(shí)現(xiàn)的具體思路
這篇文章主要給大家介紹了關(guān)于android聊天界面鍵盤、表情切換絲滑實(shí)現(xiàn)的具體思路,具體實(shí)現(xiàn)包括在XML布局中使用FrameLayout和RecyclerView,并在代碼中進(jìn)行相應(yīng)的高度控制和事件處理,需要的朋友可以參考下2024-12-12
android實(shí)現(xiàn)短按電源鍵關(guān)機(jī)的實(shí)現(xiàn)代碼
這篇文章主要介紹了android實(shí)現(xiàn)短按電源鍵關(guān)機(jī)的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
Android自定義view實(shí)現(xiàn)阻尼效果的加載動畫
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)阻尼效果的加載動畫的相關(guān)資料,非常不錯,具有一定的參考借鑒加載,需要的朋友可以參考下2016-11-11
Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁面切換效果
這篇文章主要介紹了Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁面切換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Android圖片加載框架Coil的詳細(xì)使用總結(jié)
Coil是Android上的一個全新的圖片加載框架,它的全名叫做coroutine image loader,即協(xié)程圖片加載庫,下面這篇文章主要給大家介紹了關(guān)于Android圖片加載框架Coil詳細(xì)使用的相關(guān)資料,需要的朋友可以參考下2022-07-07
Android ActionBar完全解析使用官方推薦的最佳導(dǎo)航欄(上)
Action Bar是一種新増的導(dǎo)航欄功能,在Android 3.0之后加入到系統(tǒng)的API當(dāng)中,它標(biāo)識了用戶當(dāng)前操作界面的位置,并提供了額外的用戶動作、界面導(dǎo)航等功能2017-04-04
flutter中build.gradle倉庫的配置(解決外網(wǎng)下載速度過慢失敗的問題)
這篇文章主要介紹了flutter中build.gradle倉庫的配置,解決外網(wǎng)下載速度過慢,失敗的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05

