2016-02-26 25 views
0

大家好,我正在尝试学习如何在Android中使用AlarmManagerBroadcastReceiver。 我在AlarmManager: 有一些问题我在1分钟的距离设置了两个闹钟,但只有一个火灾,并且它迟了几分钟(我猜不可预测)。AlarmManager火迟了或根本不开火

这是我的主要活动(我通过点击一个按钮设置报警)

public class MainActivity extends AppCompatActivity { 
    AlarmManager alarmManager; 
    Intent intent; 
    PendingIntent pendingIntent; 
    AtomicInteger atomicInteger; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     intent = new Intent(Constants.EXTENDED_DATA_STATUS); 
     atomicInteger = new AtomicInteger(); 
     setContentView(R.layout.activity_main); 

     Button startButton = (Button) findViewById(R.id.start_button); 

     startButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       int id = atomicInteger.incrementAndGet(); 
       pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),id,intent,0); 
       Calendar firstLullo = Calendar.getInstance(); 
       Calendar secondLullo = Calendar.getInstance(); 
       firstLullo.set(Calendar.HOUR_OF_DAY,10); 
       firstLullo.set(Calendar.MINUTE,55); 
       secondLullo.set(Calendar.HOUR_OF_DAY,10); 
       secondLullo.set(Calendar.MINUTE,56); 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
        alarmManager.setExact(AlarmManager.RTC_WAKEUP,firstLullo.getTimeInMillis(),pendingIntent); 
        alarmManager.setExact(AlarmManager.RTC_WAKEUP,secondLullo.getTimeInMillis(),pendingIntent); 
       } 
       Log.d("ALARM","settato con id " + String.valueOf(id)); 
      } 
     }); 
    } 
} 

我的收款人只显示一个面包和让手机震动。 Constants.EXTENDED_DATA_STATUS是一个Constants类的字符串,它被添加到Android Manifest中我的Reveiver的intent-filter

我在想什么?我用Google搜索了很多,只发现我必须使用setExact(),但没有运气..

在此先感谢

+0

能否请您发表您的'的onReceive()'你报警的一部分? – Strider

回答

1

是的,当然上面的代码只会触发一个闹钟,因为您为两个闹钟设置了相同的ID。

在pendingintent中通过它们的id来识别和区分报警。

使用不同的ID为不同的警报

更新代码:

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1234 ,intent,0); 
alarmManager.setExact(AlarmManager.RTC_WAKEUP,firstLullo.getTimeInMillis(),pendingIntent); 

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 5678, intent,0); 
alarmManager.setExact(AlarmManager.RTC_WAKEUP,secondLullo.getTimeInMillis(),pendingIntent); 
+0

仍然一样** pendingintent ** – Strider

+0

@Strider检查id,pendingintent值是否变化, –

+0

如何在不知情的情况下downvote,请upvote –

4

之所以只有一个火灾是因为你的取消您的第一个总是

来自文档:如果已经为同一个IntentSender安排了报警,则先前的报警将首先被取消。

要解决这个问题,请对两个警报使用不同的PendingIntents

-为了让你的接收机火多个时间,重新安排它在你的onReceive()

例子: 只有ü要分开两个接收器从海誓山盟!在

//Declare AlarmManager 
AlarmManager am = (AlarmManager) LayoutActivity.this.getSystemService(ALARM_SERVICE); 

//create new calendar instance for your first alarm 
Calendar startTime= Calendar.getInstance(); 

//set the time of your first alarm 
firstLullo.set(Calendar.HOUR_OF_DAY,10); 
firstLullo.set(Calendar.MINUTE, 55); 
firstLullo.set(Calendar.SECOND, 0); 

//create a pending intent 
PendingIntent firstPI = PendingIntent.getBroadcast(yourActivity.this, 0, new Intent("yourFirstAlarmReceiver"), PendingIntent.FLAG_UPDATE_CURRENT); 

//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day 
am.setRepeating(AlarmManager.RTC_WAKEUP, firstAlarm.getTimeInMillis(), firstPI); 

//---------------------------------------------------- 

//create new calendar instance for your second alarm 
Calendar endCalender = Calendar.getInstance(); 

//Set the time alarm of your second alarm 
secondLullo.set(Calendar.HOUR_OF_DAY,10); 
secondLullo.set(Calendar.MINUTE,56); 
secondLullo.set(Calendar.SECOND, 0); 

//create a pending intent to be 
PendingIntent secondPI= PendingIntent.getBroadcast(yourActivity.this, 0, new Intent("yourSecondAlarmReceiver"), PendingIntent.FLAG_UPDATE_CURRENT); 

//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day 
am.setRepeating(AlarmManager.RTC_WAKEUP, secondLullo.getTimeInMillis(), secondPI); 

注册报警的Manifest.xml

<receiver android:name="firstAlarm" > 
    <intent-filter> 
     <action android:name="yourFirstAlarmReceiver" > 
     </action> 
    </intent-filter> 
</receiver> 

<receiver android:name="secondAlarm" > 
    <intent-filter> 
      <action android:name="yourSecondAlarmReceiver" > 
      </action> 
    </intent-filter> 
</receiver> 

现在ü可以叫你报警,包括:

首先报警:

public class yourFirstAlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

    //Do something when first alarm goes off 

    } 
} 

第二个报警:

public class yourSecondAlarmReceiverextends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

    //Do something when second alarm goes off 

    } 
} 

应该帮助你出来。

+0

不同的PendingIntents和pendingintent具有不同的值有什么区别。当价值改变时,它就变得不同了。 –

+0

为什么我应该创建不同的接收器?我的意思是,如果待处理的意图具有不同的ID,他们为什么被认为是相同的? 如果我想发出n次报警,应该创建n个接收器吗?这对我来说看起来不太好:/ – magicleon

+0

如果你想一次使用多个警报接收器,那么这是一个**示例**,如果你希望**一个**接收器多次触发,则不需要使用它,比你应该在你的'onReceive()'中重新安排你的闹钟。我只写了这个原因,你说:'我设置了两个闹钟' – Strider