BroadcastReceiver在被调用后重启

时间:2016-12-01 10:18:59

标签: android broadcastreceiver alarmmanager

我正在使用alarmManager,从API 19开始,除了使用setExact()方法设置的警报外,所有警报都不准确。所以,如果我想设置多个确切的报警,我多次使用setExact()方法。我决定最好设置一个,然后在广播接收器中设置另一个。问题是每个BroadcastReceiver都被触发,新的警报被设置为BroadcastReceiver重新启动。

我的代码:

Calendar calendar = Calendar.getInstance();

        Date previoudTime = calendar.getTime();
        calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek-1);

        calendar.set(Calendar.HOUR, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
        String sDate = dateFormat.format(calendar.getTime());
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent intent1 = new Intent(MyService_alarm.this, MyReceiver_Alarm.class);
        Log.e("Point_1", "Compare1 " + calendar.getTime());
        Log.e("Point_1", "Compare2 " + previoudTime);
        Log.e("Point_1", "Compare " + calendar.getTime().compareTo(previoudTime));
        if(calendar.getTime().compareTo(previoudTime) < 0) {

            int a = calendar.get(Calendar.WEEK_OF_MONTH);
            calendar.set(Calendar.WEEK_OF_MONTH,a + 1);
            //Log.e("Point_1", "Less " + calendar.getTime());
        }
        Long alarmTime = calendar.getTimeInMillis();
        intent1.putExtra("Size_ABC", y);
        intent1.putExtra("Date",sDate);
        intent1.putExtra("Position",position);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MyService_alarm.this, position, intent1, 0);
        //Also change the time to 24 hours.
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);

BroadcastReceiver代码:

public class MyReceiver_Alarm extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        final MediaPlayer mediaPlayer = MediaPlayer.create(context, RingtoneManager.getActualDefaultRingtoneUri(context,RingtoneManager.TYPE_ALARM));
        SQLiteDataBase sqLiteDataBase = new SQLiteDataBase(context);
        final AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audio.setStreamVolume(AudioManager.STREAM_MUSIC,sqLiteDataBase.getVolume(intent.getIntExtra("Size_ABC",1)),0);
        final int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
        //Log.e("Point_1", "Current Volume " + currentVolume);
        //mediaPlayer.setVolume(0.50f,1.0f);
        mediaPlayer.start();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                mediaPlayer.stop();
                //Log.e("Point_1", "Volume_after " + audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
            }
        }, 5000);
        Log.e("Point_1", "Alarm!!! " + sqLiteDataBase.getVolume(intent.getIntExtra("Size_ABC",1)));
        ParsePosition parsePosition = new ParsePosition(0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
        Calendar calendar1 = Calendar.getInstance();
        Log.e("Point_1","Intent_Extra " + intent.getStringExtra("Date"));
        calendar1.setTime(simpleDateFormat.parse(intent.getStringExtra("Date"), parsePosition));
        int i = calendar1.get(Calendar.MINUTE);
        calendar1.set(Calendar.MINUTE, i + 3);
        long alarmTime = calendar1.getTimeInMillis();
        DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
        String sDate = dateFormat.format(calendar1.getTime());
        Intent intent1 = new Intent(context,MyReceiver_Alarm.class);
        intent1.putExtra("Date", sDate);
        intent1.putExtra("Position",intent.getIntExtra("Position",1));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,intent.getIntExtra("Position",1),intent1,0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
        Log.e("Point_1","SDateP " + calendar1.getTime());
    }
}

Logcat(只是重复的一部分):

Intent_Extra Thu Dec 01 03:15:00 GMT+05:00 2016
12-01 15:15:01.202  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ SDateP Thu Dec 01 03:18:00 GMT+05:00 2016
12-01 15:15:01.252  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ id 21
12-01 15:15:01.262  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ Volume 0
12-01 15:15:01.272  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ id 21
12-01 15:15:01.282  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ Volume 0
12-01 15:15:01.282  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ Alarm!!! 0
12-01 15:15:01.282  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ Intent_Extra Thu Dec 01 03:15:00 GMT+05:00 2016
12-01 15:15:01.282  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ SDateP Thu Dec 01 03:18:00 GMT+05:00 2016
12-01 15:15:01.482  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ id 21
12-01 15:15:01.482  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ Volume 0
12-01 15:15:01.512  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ id 21
12-01 15:15:01.512  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ Volume 0
12-01 15:15:01.512  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ Alarm!!! 0
12-01 15:15:01.512  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ Intent_Extra Thu Dec 01 03:15:00 GMT+05:00 2016
12-01 15:15:01.512  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ SDateP Thu Dec 01 03:18:00 GMT+05:00 2016
12-01 15:15:01.572  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ id 21
12-01 15:15:01.582  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ Volume 0
12-01 15:15:01.602  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ id 21
12-01 15:15:01.602  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ Volume 0
12-01 15:15:01.602  20321-20321/com.SetAndGet.amadey.myapplication5 E/Point_1﹕ Alarm!!! 0

这里有什么问题?如何防止这种重复?

谢谢。

1 个答案:

答案 0 :(得分:0)

我认为您的广播接收器不会重启......

我们可以通过两种方式注册我们的接收器 1.静态文件清单 2.通过registerReceiver(接收器)动态地发送;

你可能会同时使用这两件事情。 我是初学者。我想帮助你。对不起,如果我错了。 希望能帮助到你。