閱讀625 返回首頁    go 阿裏雲 go 技術社區[雲棲]


一個不錯的定時程序,寫的很好的


https://www.apkbus.com/forum.php?mod=viewthread&tid=159796

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

Screenshot_2014-01-11-12-27-49.png (94.47 KB, 下載次數: 2)

下載附件  保存到相冊

昨天 12:55 上傳

Screenshot_2014-01-11-12-29-10.png (108.85 KB, 下載次數: 2)

下載附件  保存到相冊

昨天 14:08 上傳

Screenshot_2014-01-11-12-29-40.png (61.88 KB, 下載次數: 2)

下載附件  保存到相冊

昨天 14:09 上傳

Screenshot_2014-01-11-12-29-48.png (60.52 KB, 下載次數: 2)

下載附件  保存到相冊

昨天 14:09 上傳

Screenshot_2014-01-11-14-10-35.png (100.59 KB, 下載次數: 2)

下載附件  保存到相冊

昨天 14:09 上傳

Screenshot_2014-01-11-14-11-47.png (94.58 KB, 下載次數: 2)

下載附件  保存到相冊

昨天 14:09 上傳


從各個界麵已經開到軟件設計的思路非常的清晰。首先首次運行的時候,可以把你手機的聯係人同步到軟件。可以設置你需要阻止的手機來電類型。可以設置你需要阻止來電的時間。攔截到來電以後回複的短信內容或者是郵件內容。還可以查看你攔截的電話日誌。
        代碼實現上也比較簡單:
        就是設置一個PhoneStateReceiver,監聽電話的狀態。
  1.         @Override
  2.         public void onReceive (Context context, Intent intent) {
  3.                                 
  4.                 if (QSLog.DEBUG_D)QSLog.d(CLASS_NAME, "BroadcastReceive. Tipo: " + intent.getAction());
  5.                
  6.                 //-------------                LISTEN FOR BOOT COMPLETED                ------------------//
  7.                 if (intent.getAction().equals(BOOT_COMPLETED)) {
  8.                         listenBootCompleted(context);                        
  9.                 }               
  10.                
  11.                 //----------                LISTEN FOR INCOMING CALLS                ------------------//               
  12.                 else if (intent.getAction().equals(PHONE_STATE)) {                                       

  13.                         //ITelephony telephonyService = createITelephonyImp();
  14.                         listenIncomingCalls(context);                        
  15.                 }
  16.                
  17.                 //-----------                LISTEN FOR INCOMING SMS                ----------------------//
  18.                 else if (intent.getAction().equals(SMS_RECEIVED)) {
  19.                         //listenIncomingSMS(context, intent);
  20.                 }
  21.                         
  22.         }
複製代碼


                                
自定義MyPhoneStateListener。在電話狀態變化時,進行處理。
  1. public void onCallStateChanged (int state, String incomingNumber) {
  2.                
  3.                 try {                                                                                
  4.                         
  5.                         switch (state) {                                       
  6.                         
  7.                                 //-------------                CALL_STATE_IDLE                ----------------------//
  8.                                 //Device call state: No activity.
  9.                                 case TelephonyManager.CALL_STATE_IDLE:                                                                                                                                                               
  10.                                         processCallStateIdle();                                                                                                
  11.                                         break;
  12.                                 
  13.                                 //-----------------                CALL_STATE_OFFHOOK                --------------//
  14.                                 //Device call state: Off-hook. At least one call exists that is
  15.                                 //dialing, active, or on hold, and no calls are ringing or waiting.
  16.                                 case TelephonyManager.CALL_STATE_OFFHOOK:                                       
  17.                                         processCallStateOffhook();
  18.                                         break;
  19.                                 
  20.                                 //-----------------                CALL_STATE_RINGING                --------------//
  21.                                 //Device call state: Ringing. A new call arrived and is ringing
  22.                                 //or waiting. In the latter case, another call is already active.
  23.                                 case TelephonyManager.CALL_STATE_RINGING:                                                        
  24.                                         processCallStateRinging(incomingNumber);
  25.                                         break;
  26.                                        
  27.                                 default:
  28.                                         break;
  29.                         }
  30.                         
  31.                 }catch (Exception e) {
  32.                         if (QSLog.DEBUG_E)QSLog.e(CLASS_NAME, ExceptionUtils.exceptionTraceToString(
  33.                                         e.toString(),
  34.                                         e.getStackTrace()));                        
  35.                 }
  36.         }               

複製代碼

                                
最後在IncomingCallOperations根據電話的來電時間是否在設置需要攔截的時間裏。進行靜音或者掛掉處理。並回複短信或者郵件。
  1. public synchronized void silentIncomingCall () {
  2.                
  3.                 try {                                                               
  4.                         
  5.                         ClientDDBB clientDDBB = new ClientDDBB();
  6.                         
  7.                         QSLog.d(CLASS_NAME, "IncomingNuber: " + incomingCallNumber);                        
  8.                                                 
  9.                         String phoneNumberWhithoutDashes =
  10.                                 TokenizerUtils.tokenizerPhoneNumber(incomingCallNumber, null);
  11.                         
  12.                         QSLog.d(CLASS_NAME, "IncomingNumberFormated: " + phoneNumberWhithoutDashes);
  13.                                 
  14.                         //create the CallLog object for log calls.
  15.                         CallLog callLog = new CallLog();
  16.                         
  17.                         //check if the call is in the interval time
  18.                         boolean isInInterval = checkSchedule(callLog, clientDDBB);
  19.                         
  20.                         if (isInInterval) {
  21.                
  22.                                 //Put the mobile phone in silent mode (sound+vibration)
  23.                                 //putRingerModeSilent();
  24.                                 
  25.                                 //End call using the ITelephony implementation                                       
  26.                                 //telephonyService.endCall();
  27.                                 
  28.                                 BCBean bcBean = processBlockedActionType(
  29.                                                 clientDDBB,                                         
  30.                                                 phoneNumberWhithoutDashes);
  31.                                 
  32.                                 if (QSLog.DEBUG_D)QSLog.d(CLASS_NAME, "BloquedContactBean: " + bcBean);
  33.                                 
  34.                                 
  35.                                 /* Check if the mail service is running, if it is true
  36.                                  * create a SendMail object for try to send one or more
  37.                                  * email to the contact with the incoming number
  38.                                  */
  39.                                 if (bcBean != null)
  40.                                         sendMail(incomingCallNumber, callLog, clientDDBB);
  41.                                 
  42.                                 /* Check if the sms service is running, if it is true
  43.                                  * create a SendSMS object for try to send a SMS to
  44.                                  * the contact with the incoming number
  45.                                  */
  46.                                 if (bcBean != null)
  47.                                         sendSMS(incomingCallNumber, callLog, clientDDBB);        
  48.                                 
  49.                                 if (bcBean != null)
  50.                                         //Save the callLog object in the ddbb if is appropriate.
  51.                                         saveCallLog(
  52.                                                 clientDDBB,
  53.                                                 callLog,
  54.                                                 incomingCallNumber,
  55.                                                 bcBean.getUsedContact());
  56.                         }
  57.                         //If the call isn't in the interval range
  58.                         else
  59.                                 if (QSLog.DEBUG_D)QSLog.d(CLASS_NAME, "No está en el intervalo");
  60.                         
  61.                         //close the ddbb.
  62.                         clientDDBB.close();
  63.                                                                                                                                                                                                                         
  64.                         
  65.                 }catch (Exception e) {
  66.                         if (QSLog.DEBUG_E)QSLog.e(CLASS_NAME, ExceptionUtils.exceptionTraceToString(
  67.                                         e.toString(),
  68.                                         e.getStackTrace()));                                
  69.                 }
  70.         }
  71.         

複製代碼

                                
quitesleep代碼都比較清晰簡單。但是裏麵用到了一個比較方便的數據存儲的方式。對象型數據庫db4o.db4o無需建表,無需寫sql直接對對象進行增刪改查操作,爽快吧。
        db4o特性
db4o的目標是提供一個功能強大的,適合嵌入的數據庫引擎,可以工作在設備,移動產品,桌麵以及服務器等各種平台。主要特性如下:
  • 開源模式。與其他 ODBMS不同,db4o為開源軟件,通過開源社區的力量驅動開發db4o產品。
  • 原生數據庫。db4o100原生的麵向對象數據庫,直接使用編程語言來操作數據庫。程序員無需進行OR映射來存儲對象,大大節省了程序員在存儲數據的開發時間。
  • 高性能。 db4o官方基準測試數據
  • fig2.jpg (19.32 KB, 下載次數: 2)

    下載附件  保存到相冊

    昨天 14:11 上傳


  •                                 
    Quitesleep中對db4o進行了封裝。ClientDDBB中有以下4個對象,分別負責進行增刪改查
            protectedSelects selects;
            protectedInserts inserts;
            protectedUpdates updates;
            protectedDeletes deletes;
            我們隻需要定義好數據對象。對數據的增刪改查就是以下這麼簡單。
    1. //新增聯係人
    2. //Create the db4o contact object.
    3.                                            Contact contact = new Contact(contactId, contactName);               
    4.                                           
    5.                                            //insert the contact object
    6.                                            clientDDBB.getInserts().insertContact(contact);


    7. //刪除聯係人
    8. ClientDDBB clientDDBB = new ClientDDBB();
    9.                
    10.                         int  deleteContacts         =         clientDDBB.getDeletes().deleteAllContacts();


    11. //更新聯係人
    12.         Contact contact = clientDDBB.getSelects().selectContactForName(contactName);
    13.                         contact.setBanned(false);
    14.                         clientDDBB.getUpdates().insertContact(contact);


    15.         //根據號碼查詢聯係人
    16.         Contact usedContact = clientDDBB.getSelects().
    17.                                 selectContactForPhoneNumber(incomingNumber);


    18.         

    複製代碼
    源碼: quitesleep.zip (4.95 MB, 下載次數: 17)
    昨天 14:20 上傳
    點擊文件名下載附件
    下載積分: 下載豆 -2

最後更新:2017-04-03 12:54:29

  上一篇:go 了解自己
  下一篇:go android客戶端和服務端js交互