通知时间

时间:2013-12-03 12:43:46

标签: android android-notifications android-alarms

下午好。我想在17:00收到通知。我正在使用Notification和AlarmManager,但它不起作用。

LoginActivity中的OnCreate(它在应用启动时调用)

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MONTH, 12);
    calendar.set(Calendar.YEAR, 2013);
    calendar.set(Calendar.DAY_OF_MONTH, 3);

    calendar.set(Calendar.HOUR_OF_DAY, 17);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 13);

    calendar.set(Calendar.AM_PM, Calendar.PM);

    Intent myIntent = new Intent(LoginActivity.this, MyReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(LoginActivity.this, 0,myIntent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

MyReceiver:

public class MyReceiver extends BroadcastReceiver {

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

    Intent service1 = new Intent(context, MyAlarmService.class);
    context.startService(service1);

 }
}

MyAlarmService

public class MyAlarmService extends Service {

private NotificationManager mManager;

@Override
public IBinder onBind(Intent intent) { return null; }

@Override
public void onCreate() { super.onCreate(); }

@Override
public void onDestroy() { super.onDestroy(); }


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    super.onStartCommand(intent, flags, startId);
    mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(), LoginActivity.class);
    Notification notification = new Notification.Builder(this.getApplicationContext()).
            setContentTitle("Texter").setContentText("OK").setSmallIcon(R.drawable.ic_launcher).build();
    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;

    mManager.notify(0, notification);

    return startId;


} }

在清单

 <service android:name=".MyAlarmService"
        android:enabled="true"></service>
    <receiver  android:name=".MyReceiver"></receiver>

感谢。

1 个答案:

答案 0 :(得分:1)

你应该设置:

calendar.set(Calendar.MONTH,11); 要么  calendar.set(Calendar.MONTH,Calender.DECEMBER);

月份的编号从0开始

相关问题