您的代码似乎是确定。 可能有问题的唯一情况是间隔4 * 60 * 60太短,即14.4秒。
此外,你似乎并没有将意图指向特定的接收器。你应该这样做:
Calendar calendar = Calendar.getInstance();
Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)this.getSystemService(this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60, pendingIntent);
你应该抓住它在:
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, EVentsPerform.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.applogo)
.setContentTitle("text")
.setContentText("text")
.setWhen(when)
.setContentIntent(pendingIntent)
notificationManager.notify(yourId, mNotifyBuilder.build());
}
}
添加到您的清单文件:
<!-- permission required to use Alarm Manager -->
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<!-- Register the Alarm Receiver -->
<receiver android:name="com.example.alarmmanagernotifcation.AlarmReceiver"/>
您使用的报警管理和工作Scheduler.Post你的代码你试过了。 –
是的!我用过。 – Komal
已更新代码! – Komal