如何根据时间安排个人资料?

时间:2016-03-02 07:39:36

标签: android

在我的项目中,我想根据时间安排音频配置文件。 从用户接收调度的开始和结束时间,并且该数据存储在SQLite数据库中。 我完成了所有这些事情。 我创建了Intent Service类以获得当前时间。 在服务类中,我获取存储在数据库中的时间并将其与当前时间进行比较。如果两者都匹配,那么我检查用户给出的配置文件模式,并根据我使用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();
            }

1 个答案:

答案 0 :(得分:0)

我认为您应该为此任务使用警报管理器。设置重复闹钟24小时。并在用户更新时更新警报管理器,假设用户设置启动时间是早上6点,然后是早上6点,警报管理器会通知您现在可以启动。它为您节省了很多处理服务的麻烦。检查时间与当前时间的不必要的比较。我希望它会对你有所帮助!祝你有美好的一天!

相关问题