一个不错的定时程序,写的很好的

来源:互联网 发布:java使用框架的好处 编辑:程序博客网 时间:2024/05/20 07:15

http://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个对象,分别负责进行增删改查
            protectedSelectsselects;
            protectedInsertsinserts;
            protectedUpdatesupdates;
            protectedDeletesdeletes;
            我们只需要定义好数据对象。对数据的增删改查就是以下这么简单。
    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
0 0