一個不錯的定時程序,寫的很好的
“本文參與趣米杯征文活動,如需轉載請注明出處和作者”。
quitesleep是一款android手機的小軟件。它可以設定在你睡覺的時候,當有電話打進來的時候。自動掛斷或者設定為靜音,並給打電話的人回複你之前設定好的郵件或者短信。保證你睡覺的時候不受打擾。
軟件運行界麵如下:






從各個界麵已經開到軟件設計的思路非常的清晰。首先首次運行的時候,可以把你手機的聯係人同步到軟件。可以設置你需要阻止的手機來電類型。可以設置你需要阻止來電的時間。攔截到來電以後回複的短信內容或者是郵件內容。還可以查看你攔截的電話日誌。
代碼實現上也比較簡單:
就是設置一個PhoneStateReceiver,監聽電話的狀態。
- @Override
- public void onReceive (Context context, Intent intent) {
-
- if (QSLog.DEBUG_D)QSLog.d(CLASS_NAME, "BroadcastReceive. Tipo: " + intent.getAction());
-
- //------------- LISTEN FOR BOOT COMPLETED ------------------//
- if (intent.getAction().equals(BOOT_COMPLETED)) {
- listenBootCompleted(context);
- }
-
- //---------- LISTEN FOR INCOMING CALLS ------------------//
- else if (intent.getAction().equals(PHONE_STATE)) {
- //ITelephony telephonyService = createITelephonyImp();
- listenIncomingCalls(context);
- }
-
- //----------- LISTEN FOR INCOMING SMS ----------------------//
- else if (intent.getAction().equals(SMS_RECEIVED)) {
- //listenIncomingSMS(context, intent);
- }
-
- }
自定義MyPhoneStateListener。在電話狀態變化時,進行處理。
- public void onCallStateChanged (int state, String incomingNumber) {
-
- try {
-
- switch (state) {
-
- //------------- CALL_STATE_IDLE ----------------------//
- //Device call state: No activity.
- case TelephonyManager.CALL_STATE_IDLE:
- processCallStateIdle();
- break;
-
- //----------------- CALL_STATE_OFFHOOK --------------//
- //Device call state: Off-hook. At least one call exists that is
- //dialing, active, or on hold, and no calls are ringing or waiting.
- case TelephonyManager.CALL_STATE_OFFHOOK:
- processCallStateOffhook();
- break;
-
- //----------------- CALL_STATE_RINGING --------------//
- //Device call state: Ringing. A new call arrived and is ringing
- //or waiting. In the latter case, another call is already active.
- case TelephonyManager.CALL_STATE_RINGING:
- processCallStateRinging(incomingNumber);
- break;
-
- default:
- break;
- }
-
- }catch (Exception e) {
- if (QSLog.DEBUG_E)QSLog.e(CLASS_NAME, ExceptionUtils.exceptionTraceToString(
- e.toString(),
- e.getStackTrace()));
- }
- }
最後在IncomingCallOperations根據電話的來電時間是否在設置需要攔截的時間裏。進行靜音或者掛掉處理。並回複短信或者郵件。
- public synchronized void silentIncomingCall () {
-
- try {
-
- ClientDDBB clientDDBB = new ClientDDBB();
-
- QSLog.d(CLASS_NAME, "IncomingNuber: " + incomingCallNumber);
-
- String phoneNumberWhithoutDashes =
- TokenizerUtils.tokenizerPhoneNumber(incomingCallNumber, null);
-
- QSLog.d(CLASS_NAME, "IncomingNumberFormated: " + phoneNumberWhithoutDashes);
-
- //create the CallLog object for log calls.
- CallLog callLog = new CallLog();
-
- //check if the call is in the interval time
- boolean isInInterval = checkSchedule(callLog, clientDDBB);
-
- if (isInInterval) {
-
- //Put the mobile phone in silent mode (sound+vibration)
- //putRingerModeSilent();
-
- //End call using the ITelephony implementation
- //telephonyService.endCall();
-
- BCBean bcBean = processBlockedActionType(
- clientDDBB,
- phoneNumberWhithoutDashes);
-
- if (QSLog.DEBUG_D)QSLog.d(CLASS_NAME, "BloquedContactBean: " + bcBean);
-
-
- /* Check if the mail service is running, if it is true
- * create a SendMail object for try to send one or more
- * email to the contact with the incoming number
- */
- if (bcBean != null)
- sendMail(incomingCallNumber, callLog, clientDDBB);
-
- /* Check if the sms service is running, if it is true
- * create a SendSMS object for try to send a SMS to
- * the contact with the incoming number
- */
- if (bcBean != null)
- sendSMS(incomingCallNumber, callLog, clientDDBB);
-
- if (bcBean != null)
- //Save the callLog object in the ddbb if is appropriate.
- saveCallLog(
- clientDDBB,
- callLog,
- incomingCallNumber,
- bcBean.getUsedContact());
- }
- //If the call isn't in the interval range
- else
- if (QSLog.DEBUG_D)QSLog.d(CLASS_NAME, "No está en el intervalo");
-
- //close the ddbb.
- clientDDBB.close();
-
-
- }catch (Exception e) {
- if (QSLog.DEBUG_E)QSLog.e(CLASS_NAME, ExceptionUtils.exceptionTraceToString(
- e.toString(),
- e.getStackTrace()));
- }
- }
-
quitesleep代碼都比較清晰簡單。但是裏麵用到了一個比較方便的數據存儲的方式。對象型數據庫db4o.db4o無需建表,無需寫sql直接對對象進行增刪改查操作,爽快吧。
db4o特性
db4o的目標是提供一個功能強大的,適合嵌入的數據庫引擎,可以工作在設備,移動產品,桌麵以及服務器等各種平台。主要特性如下:
-
開源模式。與其他 ODBMS不同,db4o為開源軟件,通過開源社區的力量驅動開發db4o產品。
-
原生數據庫。db4o是100原生的麵向對象數據庫,直接使用編程語言來操作數據庫。程序員無需進行OR映射來存儲對象,大大節省了程序員在存儲數據的開發時間。
-
高性能。 db4o官方基準測試數據
-
Quitesleep中對db4o進行了封裝。ClientDDBB中有以下4個對象,分別負責進行增刪改查protectedSelects selects;protectedInserts inserts;protectedUpdates updates;protectedDeletes deletes;我們隻需要定義好數據對象。對數據的增刪改查就是以下這麼簡單。 -
- //新增聯係人
- //Create the db4o contact object.
- Contact contact = new Contact(contactId, contactName);
-
- //insert the contact object
- clientDDBB.getInserts().insertContact(contact);
- //刪除聯係人
- ClientDDBB clientDDBB = new ClientDDBB();
-
- int deleteContacts = clientDDBB.getDeletes().deleteAllContacts();
- //更新聯係人
- Contact contact = clientDDBB.getSelects().selectContactForName(contactName);
- contact.setBanned(false);
- clientDDBB.getUpdates().insertContact(contact);
- //根據號碼查詢聯係人
- Contact usedContact = clientDDBB.getSelects().
- selectContactForPhoneNumber(incomingNumber);
-
quitesleep.zip (4.95 MB, 下載次數: 17)
昨天 14:20 上傳點擊文件名下載附件
下載積分: 下載豆 -2 - //新增聯係人
最後更新:2017-04-03 12:54:29