带有广播接收器的报警管理器(接收器中的Toast未显示)

时间:2014-03-08 18:53:32

标签: android

我在这里做错了什么?我在清单中有setalarm和wakelock。我是Alarm Manager的初学者

public class AlarmManagerActivity extends Activity {

      @Override
      public void onCreate(Bundle savedInstanceState) 
     {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_alarm_manager);
     }

   public void scheduleAlarm(View V)
   {

           Intent intentAlarm = new Intent(this, AlarmReceiverActivity.class);

           // create the object
           AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

           //set the alarm for particular time
           alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));

   }

}


public class AlarmReceiverActivity extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent)
      {
          System.out.println("YO!");
              Toast.makeText(context, "Alarm Triggered in ", Toast.LENGTH_LONG).show();
       }

}

请帮帮我,我的目标是每隔早上6点用闹钟管理器更新我的数据库。这就像重置一个值,所以我限制用户活动1天

1 个答案:

答案 0 :(得分:1)

我敢打赌你忘了注册BroadcastReceiver。您还需要:

registerReceiver(new AlarmReceiverActivity(), filter);

或者,在你的情况下,在Manifest中可能更好

<receiver android:name="your.package.AlarmReceiverActivity">
  <filter...
</receiver>