2016-03-02 24 views
0

在我的項目中,我想根據時間安排音頻配置文件。 從用戶接收調度的開始和結束時間,並且該數據存儲在SQLite數據庫中。 我完成了所有這些事情。 我創建了Intent Service類來獲取當前時間。 在服務類I中,獲取存儲在數據庫中的時間並將其與當前時間進行比較。如果兩者都匹配,那麼我檢查用戶給出的配置文件模式,並根據該模式使用AudioManager類來更改模式。 但是調度沒有完成。如何根據時間安排配置文件?

int HF1,MF1,HT1,MT1; 
         String hf1,mf1,ht1,mt1,profile1; 
         hf1=""; 
         mf1=""; 
         ht1=""; 
         mt1=""; 
         profile1=""; 
         String hf,mf,ht,mt; 
         Intent intentF1S=new Intent(getBaseContext(),SilentBroadcast.class); 
         final PendingIntent senderF1S=PendingIntent.getBroadcast(this, 192837, intentF1S, PendingIntent.FLAG_UPDATE_CURRENT); 


         Intent intentT1S=new Intent(getBaseContext(),NormalBroadcast.class); 
         final PendingIntent senderT1S=PendingIntent.getBroadcast(this, 192837, intentT1S, PendingIntent.FLAG_UPDATE_CURRENT); 


         Intent intentF1V=new Intent(getBaseContext(),VibrateBroadcast.class); 
         final PendingIntent senderF1V=PendingIntent.getBroadcast(this, 192837, intentF1V, PendingIntent.FLAG_UPDATE_CURRENT); 


         Intent intentT1V=new Intent(getBaseContext(),NormalBroadcast.class); 
         final PendingIntent senderT1V=PendingIntent.getBroadcast(this, 192837, intentT1V, PendingIntent.FLAG_UPDATE_CURRENT); 


         mHelper=new DbHelper(getBaseContext()); 
         mHelper.open(); 
         Cursor c=mHelper.returnDatat(); 
         if (c.moveToFirst()) { 
          do { 
           hf1=(c.getString(c.getColumnIndex(DbHelper.KEY_HFROM))); 
           mf1=(c.getString(c.getColumnIndex(DbHelper.KEY_MFROM))); 
           ht1=(c.getString(c.getColumnIndex(DbHelper.KEY_HTO))); 
           mt1=(c.getString(c.getColumnIndex(DbHelper.KEY_MTO))); 
           profile1=(c.getString(c.getColumnIndex(DbHelper.KEY_PROFILE))); 

           try 
           { 
            HF1=Integer.parseInt(hf1.toString()); 
            HT1=Integer.parseInt(ht1.toString()); 

            MF1=Integer.parseInt(mf1.toString()); 
            MT1=Integer.parseInt(mt1.toString()); 

          Calendar cf1=Calendar.getInstance(); 
          cf1.set(Calendar.HOUR, HF1); 
          cf1.set(Calendar.MINUTE, MF1); 


          Calendar ct1=Calendar.getInstance(); 
          ct1.set(Calendar.HOUR, HT1); 
          ct1.set(Calendar.MINUTE, MT1); 




          if(profile1.contains("Silent")) 
          { 
           AlarmManager amf1=(AlarmManager)getSystemService(ALARM_SERVICE); 
           amf1.set(AlarmManager.RTC_WAKEUP, cf1.getTimeInMillis(), senderF1S); 

           AlarmManager amt1=(AlarmManager)getSystemService(ALARM_SERVICE); 
           amt1.set(AlarmManager.RTC_WAKEUP, ct1.getTimeInMillis(), senderT1S); 


          } 
          else if(profile1.contains("Vibrate")) 
          { 

           AlarmManager amf1=(AlarmManager)getSystemService(ALARM_SERVICE); 
           amf1.set(AlarmManager.RTC_WAKEUP, cf1.getTimeInMillis(), senderF1V); 

           AlarmManager amt1=(AlarmManager)getSystemService(ALARM_SERVICE); 
           amt1.set(AlarmManager.RTC_WAKEUP, ct1.getTimeInMillis(), senderT1V); 

          } 

          else 
          { 
           AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
           am.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 
          } 
          } 


          }catch(Exception e) 
          { 

          } 



        }while (c.moveToNext()); 


       mHelper.close(); 
      } 

回答

0

我想你應該爲這個任務使用一個報警管理器。設置24小時重複報警。並在用戶更新時讓用戶設置啓動時間更新報警管理器爲上午6點,然後上午6點。警報管理器通知您現在可以啓動。它可以爲您節省很多處理服務的麻煩。和不必要的比較檢查時間與當前時間。我希望它會幫助你!祝你有美好的一天!

+0

我也是這樣做的東西 –

+0

在我的服務類中,我獲取配置文件的值,然後根據配置文件的值調用適當的AlaramManger類。但它也沒有計劃。 –

+0

我可以顯示代碼嗎? –

相關問題