我想每天设置3次闹钟,并且该闹钟应该发送3个不同的不同通知。我已经尝试了这个tutorial。每当我设置第二个警报应用程序仅发送第二个警报通知时,本教程帮助我设置1个警报。
现在我正在分享我的代码。我尝试了这个代码,并且它正好运行1次警报。
MainActivity
公共类AlarmManagerMainActivity扩展了Activity {
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_manager_main);
// Calendar calendar = Calendar.getInstance();
/*calendar.set(Calendar.MONTH, 2);
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.DAY_OF_MONTH, 12);
calendar.set(Calendar.HOUR_OF_DAY,4);
calendar.set(Calendar.MINUTE, 10);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);*/
/*Date dat = new Date();//initializes to now
Calendar cal_alarm = Calendar.getInstance();
Calendar cal_now = Calendar.getInstance();
cal_now.setTime(dat);
cal_alarm.setTime(dat);
cal_alarm.set(Calendar.HOUR_OF_DAY,4);//set the alarm time
cal_alarm.set(Calendar.MINUTE,20);
cal_alarm.set(Calendar.SECOND,0);
if(cal_alarm.before(cal_now)){//if its in the past increment
cal_alarm.add(Calendar.DATE,1);
}
Intent myIntent = new Intent(AlarmManagerMainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(AlarmManagerMainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, cal_alarm.getTimeInMillis(), pendingIntent);
*/
Intent myIntent = new Intent(AlarmManagerMainActivity.this , MyReceiver.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(AlarmManagerMainActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,10);
calendar.set(Calendar.MINUTE,15);
calendar.set(Calendar.SECOND,00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent); //set repeating every 24 hours
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.alarm_manager_main, menu);
return true;
}
}
服务:
public class MyAlarmService扩展Service { private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Toast.makeText(this, "IN Service", Toast.LENGTH_LONG).show();
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),AlarmManagerMainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
mManager.notify(0, notification);
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
接收器:
公共类MyReceiver扩展了BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "IN REceiver", Toast.LENGTH_LONG).show();
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
答案 0 :(得分:0)
如果我理解您要做的事情,那么您提供的所有通知都具有相同的ID(0),因此它们将全部替换之前的
mManager.notify(0, notification);
您需要一种区分通知并为其提供不同ID的方法。
答案 1 :(得分:0)
最后我完成了这个练习。
AlarmService
public int onStartCommand(Intent intent, int flags, int startId) {
final Handler handler = new Handler();
final Runnable r = new Runnable()
{
public void run()
{
// code here what ever is required
Calendar cal = Calendar.getInstance();
hr = cal.get(Calendar.HOUR_OF_DAY);
minute= cal.get(Calendar.MINUTE);
sec= cal.get(Calendar.SECOND);
if(hr==10 && minute==00){
flagInt=1;
sendNotification(1);
}else if(hr==20 && minute==00){
flagInt=2;
sendNotification(2);
}else if(hr==22 && minute==00){
flagInt=3;
sendNotification(3);
}else if(hr==23 && minute==00){
SharedPreferences pref = getApplicationContext().getSharedPreferences(ACTIVITY_PREF_NAME, 0);
Editor editor = pref.edit();
editor.putBoolean(IS_ACTIVITY_CHANGE, false);
editor.commit(); // commit changes
}
handler.postDelayed(this, 60000);
}
};
handler.postDelayed(r,60000);
return START_STICKY;
}