应用程序不会在指定时间静音

时间:2017-07-17 14:39:21

标签: java android alarmmanager

更新:终于开始测试了。没有工作。改变了一些代码并明天再试一次。

在我的MainActivity中,我让程序从JSON数据中检索时间数据并将它们存储到变量中。然后我调用该方法并将6个不同时间中的每一个分成几小时和几分钟,然后将它们相乘,使它们以毫秒为单位,然后在AlarmManager.RTC_WAKEUP中使用它们。这是MainActivity代码:

   mPrayerTime = (Button) findViewById(R.id.muteButton);
    mPrayerTime.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            new fajrModel();
            new shurooqModel();
            new dhuhrModel();
            new asrModel();
            new maghribModel();
            new ishaModel();


            AlarmManager falarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            AlarmManager salarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            AlarmManager dalarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            AlarmManager aalarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            AlarmManager malarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            AlarmManager ialarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

            Intent intent = new Intent(MainActivity.this, AlarmReciever.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);




            //setting up calender for times.
            Calendar fcal = Calendar.getInstance();
            SimpleDateFormat fTime = new SimpleDateFormat("h:mm a", Locale.ENGLISH);
            try {
                fcal.setTime(fTime.parse(fajr));
            } catch (ParseException e) {
                e.printStackTrace();
            }


            Calendar dcal = Calendar.getInstance();
            SimpleDateFormat dTime = new SimpleDateFormat("h:mm a", Locale.ENGLISH);
            try {
                dcal.setTime(dTime.parse(dhuhr));
            } catch (ParseException e) {
                e.printStackTrace();
            }


            Calendar scal = Calendar.getInstance();
            SimpleDateFormat sTime = new SimpleDateFormat("h:mm a", Locale.ENGLISH);
            try {
                scal.setTime(sTime.parse(shurooq));
            } catch (ParseException e) {
                e.printStackTrace();
            }


            Calendar acal = Calendar.getInstance();
            SimpleDateFormat aTime = new SimpleDateFormat("h:mm a", Locale.ENGLISH);
            try {
                acal.setTime(aTime.parse(asr));
            } catch (ParseException e) {
                e.printStackTrace();
            }


            Calendar mcal = Calendar.getInstance();
            SimpleDateFormat mTime = new SimpleDateFormat("h:mm a", Locale.ENGLISH);
            try {
                mcal.setTime(mTime.parse(maghrib));
            } catch (ParseException e) {
                e.printStackTrace();
            }


            Calendar ical = Calendar.getInstance();
            SimpleDateFormat iTime = new SimpleDateFormat("h:mm a", Locale.ENGLISH);
            try {
                ical.setTime(iTime.parse(isha));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            Log.d("fajr", String.valueOf(ical));


            /*FAJR

            */
            falarmManager.setExact(AlarmManager.RTC_WAKEUP, fcal.getTimeInMillis(), pendingIntent);

            //SHUROOQ

            salarmManager.setExact(AlarmManager.RTC_WAKEUP, scal.getTimeInMillis(), pendingIntent);

            //Dhuhr

            dalarmManager.setExact(AlarmManager.RTC_WAKEUP, dcal.getTimeInMillis(), pendingIntent);

            //ASR

            aalarmManager.setExact(AlarmManager.RTC_WAKEUP, acal.getTimeInMillis(), pendingIntent);

            //MAGHRIB

            malarmManager.setExact(AlarmManager.RTC_WAKEUP, mcal.getTimeInMillis(), pendingIntent);

            //ISHA
            ialarmManager.setExact(AlarmManager.RTC_WAKEUP, ical.getTimeInMillis(), pendingIntent);


        }


    });
}

AlarmReceiver.java:

public class AlarmReciever extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
    Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
    AudioManager silent = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    silent.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}

}

没有这样的事情。它会在那些时候静音手机,并且为了能够测试它我必须构建APK并将其放在我的手机上并等到指定的时间并且没有任何反应。我哪里出错了

TLDR:代码根本不会将手机静音,即使它应该在特定时间。

2 个答案:

答案 0 :(得分:0)

我想提醒你两件事:

1,我建议您使用SimpleDateFormat获取小时,分钟,秒等,例如:

    String minute=new SimpleDateFormat("mm").format(date);

2,我没有看到你的代码,但是,我知道AlarmManager没有按时执行,它会在你的设置或之前执行,这是为了节省电力和API 19。

答案 1 :(得分:0)

更改以下行

alarmManager.set(AlarmManager.RTC_WAKEUP, Long.parseLong(String.valueOf(totalFTime)), pendingIntent);

alarmManager.setExact(AlarmManager.RTC_WAKEUP, Long.parseLong(String.valueOf(totalFTime)), pendingIntent);