Android編程使用Service實現(xiàn)Notification定時發(fā)送功能示例
本文實例講述了Android編程使用Service實現(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));
}
};
}
/**
* 實現(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ā)通知、清楚通知等
private NotificationManager mNM;
// 使用Layout文件的對應(yīng)ID來作為通知的唯一識別
private static int MOOD_NOTIFICATIONS = R.layout.status_bar_notifications;
/**
* Android給我們提供ConditionVariable類,用于線程同步。提供了三個方法block()、open()、close()。 void
* block() 阻塞當前線程,直到條件為open 。 void block(long timeout)阻塞當前線程,直到條件為open或超時
* void open()釋放所有阻塞的線程 void close() 將條件重置為close。
*/
private ConditionVariable mCondition;
@Override
public void onCreate() {
// 狀態(tài)欄通知的管理類對象,負責發(fā)通知、清楚通知等
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 啟動一個新個線程執(zhí)行任務(wù),因Service也是運行在主線程,不能用來執(zhí)行耗時操作
Thread notifyingThread = new Thread(null, mTask, "NotifyingService");
mCondition = new ConditionVariable(false);
notifyingThread.start();
}
@Override
public void onDestroy() {
// 取消通知功能
mNM.cancel(MOOD_NOTIFICATIONS);
// 停止線程進一步生成通知
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);
// 當點擊通知時通過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)
* 通知。它將永遠是一個獨特的號碼在你的 應(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來實現(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ā)入門與進階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android入門之Service的使用詳解
- Android?NotificationListenerService通知監(jiān)聽服務(wù)使用
- Android Google AutoService框架使用詳解
- Android使用Service實現(xiàn)IPC通信的2種方式
- 說說在Android如何使用服務(wù)(Service)的方法
- Android使用Service實現(xiàn)簡單音樂播放實例
- 淺談Android中Service的注冊方式及使用
- Android 通知使用權(quán)(NotificationListenerService)的使用
- Android Service功能使用示例代碼
相關(guān)文章
Android Studio 導入開源項目的正確姿勢及注意事項
這篇文章主要介紹了Android Studio 導入開源項目的正確姿勢及注意事項,需要的朋友參考下吧2018-03-03
Android實現(xiàn)TextView字符串關(guān)鍵字變色的方法
這篇文章顯示給大家介紹了字符串中關(guān)鍵字變色的實現(xiàn)方法,而后又拓展介紹了在Android中如何實現(xiàn)搜索關(guān)鍵字變色,相信對各位Android開發(fā)者們具有一定的參考借鑒價值,感興趣的朋友們下面來一起看看吧。2016-10-10
Android使用CountDownTimer模擬短信驗證倒計時
這篇文章主要為大家詳細介紹了Android使用CountDownTimer模擬短信驗證倒計時,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
詳解Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法
本篇文章主要介紹了Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-11-11
Android編程連接MongoDB及增刪改查等基本操作示例
這篇文章主要介紹了Android編程連接MongoDB及增刪改查等基本操作,簡單介紹了MongoDB功能、概念、使用方法及Android操作MongoDB數(shù)據(jù)庫的基本技巧,需要的朋友可以參考下2017-07-07

